JPA 2 Criteria Day from date - oracle

I have a field in a table and that stores a date. I'd like to select all records that which the date is on day 5.
After a lot of research I get the code below:
Predicate dayValue = cb.equal(cb.function("day", Integer.class, test.<Date>get(Test_.dateInit)), now.get(Calendar.DAY_OF_MONTH) );
But, I'm using a Oracle database and it doesn't have the function day:
[EL Warning]: 2013-01-14 11:51:08.001--UnitOfWork(23011228)--Thread(Thread[main,5,main])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-00904: "DAY": invalid identifier
Is there an other way to do this select?
Thanks

I got a way.
Using the function to_char of oracle in the function method:
Predicate dayValue = cb.equal(cb.function("to_char", Integer.class, test.<Date>get(Test_.dateInit),cb.parameter(String.class, "dayFormat")), now.get(Calendar.DAY_OF_MONTH) );
...
q.setParameter("dayFormat", "DD");

In Oracle you can use
select to_char (date '2013-01-14', 'D') d from dual;
to give you the day number, the above will output 1 for Monday
if you want to see Monday just change to
select to_char (date '2013-01-14', 'Day') d from dual;
the above will output Monday
hope that helps.

Related

Power BI Oracle OLEDB need a where clause to get the first day of the year

using Power BI to connect to Oracle database server 19c using this connection: provider=OraOLEDB.Oracle.1;data source=gencdbp.domain.pri:1521/GAEN
I need a SQL statement that limits the rows so I don't have to hardcode it.
This works:
select * from aTable where DateCreated >= TO_Date('01/01/2022', 'MM/DD/YYYY')
I have tried the Year function and TRUNC function. Both of these gives me an error:
DateCreated is a Timestamp(6). These do not work:
where DateCreated >= trunc(sysdate, 'YEAR')
where trunc(DateCreated, 'YEAR') = trunc(sysdate, 'YEAR')
YEAR(DateCreated) = YEAR(sysdate)
These all give me this error:
ORA-00904: "YEAR": invalid identifier
Details:
DataSourceKind=OleDb
DataSourcePath=data source=gencdbp.domain.pri:1521/GAEN;provider=OraOLEDB.Oracle.1
Message=ORA-00904: "YEAR": invalid identifier
ORA-00904: "YEAR": invalid identifier
ErrorCode=-2147217900
I don't know tool you use (Power BI), but - have a look at the following example:
Sample table that contains a column whose datatype matches the one you specified:
SQL> create table test (datecreated timestamp(6));
Table created.
Insert sample value:
SQL> insert into test values (systimestamp);
1 row created.
OK, let's now try something you said doesn't work (but raises ORA-00904):
SQL> select * from test where trunc(datecreated, 'year') = trunc(sysdate, 'year');
DATECREATED
---------------------------------------------------------------------------
11-MAY-22 09.47.30.544000 PM
It works OK for me.
Another option might be the extract function:
SQL> select * from test where extract(year from datecreated) = extract(year from sysdate);
DATECREATED
---------------------------------------------------------------------------
11-MAY-22 09.47.30.544000 PM
So, are you sure you really used code you posted? Because, that's the error which suggests that there's no column named year in that table. To simulate it, I removed single quotes for year (which is - for the to_char function - format model):
SQL> select * from test where trunc(datecreated, year) = trunc(sysdate, year);
select * from test where trunc(datecreated, year) = trunc(sysdate, year)
*
ERROR at line 1:
ORA-00904: "YEAR": invalid identifier
SQL>
Right; here it is, ORA-00904.

Oracle - trunc(DateTime) between trunc(Sysdate - 104) and trunc(Sysdate - 75)

I want to select few data from a Oracle table of June month(June 1st to June 30th of 2017) and the start date will be from tomorrow(Sep 13th). Hence I wrote a query in below format,
select * from table where column1='Data1' and Column2='Data2'
and trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75)
I'm not able to check this query as I don't have tool for this. I just wrote it in notepad and want to share to my friend.
My Question - Will trunc(DateTime) between trunc(sysdate-104) and trunc(sysdate-75) condition will give data between June1st to June31 or Does any Syntax issue there?
There is no problem with the syntax itself even though your formulation is time sensitive, which means that tomorrow it won't return the same result.
Instead, go with something like this :
AND TRUNC(DateTime) BETWEEN to_date('2016-06-01','YYYY-MM-DD')
AND to_date('2016-06-30','YYYY-MM-DD')
Just cast the date format to month/year and compare against that.
select *
from table
where column1 = 'Data1'
and column2 = 'Data2'
and to_char(DateTime, 'MMYYYY') = '062017';
Hi I think the most accurate would be:
select * from table where column1='Data1' and Column2='Data2'
AND DateTime BETWEEN TRUNC(sysdate-104)
AND TRUNC(sysdate-75)
+ interval '23' hour
+ interval '59' minute
+ interval '59' second;

Oracle DB Ora-01839: date not valid for month specified. 29-02-2016 leap year

We all know today is a special day. Its the 29th of February 2016 of a leap year.
We receive an error message from some tables in our Oracle DB. The error is:Oracle ORA-01839: date not valid for month specified.
For example a simple select where the error occurs:select * from table where table_date > sysdate -0.1;
For other tables this select makes no problem, just for some of the tables.
Is there a way to fix this issue? Because we are not able to use many tables today.
We are using Oracle 12c.
After intensive research, its clear why some of our selects does not work today. The error is caused by the keyword interval and its a known issue. (Or it's how the ANSI/ISO spec says it should work, bottom of page 205/top of page 206)
Here is a qoute from the oracle community blog:
Question:
select to_date('2012-feb-29','yyyy-mon-dd') + interval '1' year as dt from dual;
ORA-01839: date not valid for month specified
01839. 00000 - "date not valid for month specified"
*Cause:
*Action:
select to_date('2012-feb-29','yyyy-mon-dd') + interval '2' year as dt from dual;
ORA-01839: date not valid for month specified
01839. 00000 - "date not valid for month specified"
*Cause:
*Action:
select to_date('2012-feb-29','yyyy-mon-dd') + interval '3' year as dt from dual;
ORA-01839: date not valid for month specified
01839. 00000 - "date not valid for month specified"
*Cause:
*Action:
select to_date('2012-feb-29','yyyy-mon-dd') + interval '4' year as dt from dual;
29-FEB-16 00:00:00
select to_date('2012-feb-29','yyyy-mon-dd') + interval '1' day as dt from dual;
01-MAR-12 00:00:00
select to_date('2012-feb-29','yyyy-mon-dd') + interval '1' month as dt from dual;
29-MAR-12 00:00:00
Answer:
That's just how INTERVALs work. Leap years are the least of the
problem; adding 1 month to March 31 results in the same error. If you
want to make sure that the result is a valid DATE, then use
ADD_MONTHS. (There's no separate function for adding years; use
ADD_MONTH (SYSDATE, 12*n) to get the DATE that is n years from now.)
Why it happens in our case:
In our case, we used virtual private database for some of our tables because of security reasons. And there we applied the interval keyword in most of the selects.
What to do instead:
Use ADD_MONTHS instead.
select add_months(to_date('2012-feb-29','yyyy-mon-dd'), 12) as dt from dual;

showing record from table year wise

select station_id,count(case_id) from emer_complaint group by station_id
Above query returning the correct result. But, when i'm trying this query for showing the record in year wise. Then it shows the error ORA-00904: "YEAR": invalid identifier
select year(date_time)
,count(case_id)
from emer_complaint
group by year(date_time);
the datatype of date_time is Timestamp.
Thanks in advance.!!
select to_char(date_time, 'YYYY'), count(case_id)
from emer_complaint
group by to_char(date_time, 'YYYY');

SSRS - Oracle DB, Passing Date parameter

Using SSRS with an Oracle Database. I need to prompt the user when running the report to enter a date for report. What is the best way to add in the parameter in my SSRS Report. Having problem finding the right date format. under the "Report Parameter" menu, I have setup the Report Parameters using the DateTime Datatype.
Keep getting this error "ORA-01843: Not a Valid Month"
Thank you for your help.
Select
a.OPR_Name,
a.OPR,
a.Trans_Desc,
a.Trans_Start_Date,
Cast(a.S_Date as date) as S_Date,
Sum(a.Duration) as T
From (
Select
US_F.OPR_Name,
ITH_F.OPR,
ITH_F.ITH_RID,
ITH_F.TRANSACT,
Transact.DESC_1 as Trans_Desc,
To_CHAR(ITH_F.Start_Time,'DD-Mon-YY') as Trans_Start_Date,
To_CHAR(ITH_F.Start_Time,'MM/DD/YYYY') as S_Date,
Substr(To_CHAR(ITH_F.Start_Time,'HH24:MI'),1,6) as Start_Time,
To_CHAR(ITH_F.End_Time,'DD-Mon-YY') as Trans_End_Date,
Substr(To_CHAR(ITH_F.End_Time,'HH24:MI'),1,6) as End_Time,
Cast(Case When To_CHAR(ITH_F.Start_Time,'DD-Mon-YY') = To_CHAR(ITH_F.End_Time,'DD-Mon-YY')
Then (((To_CHAR(ITH_F.End_Time,'SSSSS') - To_CHAR(ITH_F.Start_Time,'SSSSS')) / 60))/60
Else ((86399 - (To_CHAR(ITH_F.Start_Time,'SSSSS')) + To_CHAR(ITH_F.End_Time,'SSSSS'))/60)/60
End as Decimal(3,1)) as Duration
from Elite_76_W1.ITH_F
Left Join Elite_76_W1.Transact
on Transact.Transact = ITH_F.Transact
Left Join Elite_76_W1.US_F
on US_F.OPR = ITH_F.OPR
Where ITH_F.TRANSACT not in ('ASN','QC','LGOT')
) a
Where a.S_Date = #Event_Date
Having Sum(a.Duration) <> 0
Group By a.OPR_Name,
a.OPR,
a.Trans_Desc,
a.Trans_Start_Date,
a.S_Date
Order by a.OPR_Name
Oracle parameters are indicated with a leading colon - #Event_Date should be :Event_Date.
You use CAST(a.S_Date AS DATE) in your query, where a.S_Date is a VARCHAR: To_CHAR(ITH_F.Start_Time, 'MM/DD/YYYY'). If your session date parameter NLS_DATE_FORMAT is different from 'MM/DD/YYYY', this will result in a format error (in your case I suspect your NLS_DATE_FORMAT is something like DD-MON-YYYY, resulting in a "month" error).
A few options:
don't use TO_CHAR in the inner query (always try to keep the date format for internal calculations, use TO_CHAR only where it belongs -- in the GUI). If you only want the date portion, use TRUNC.
use TO_DATE instead of CAST in the outer query: to_date(a.S_Date, 'MM/DD/YYYY'), this is obviously tedious: you cast a date to a varchar that is later transformed to a date.

Resources