Oracle date format error - oracle

I am working on oracle table to pull the data. In one of the query i am using 'where' to filtering between date.
To_Date(VDATU,'DD-MON-YYYY') >= '03-Jun-2012'
AND To_Date(VDATU,'dd-mon-yy') <= '04-Sep-2012'
I am getting error,
ORA-01861: literal does not match format string
I tried using ('dd-mm-yy') but still it is giving the same error. what may the problem??

You are trying to convert VDATU (which is presumably a VARCHAR2) into a date.
In one place you have DD-MON-YYYY and in the other you have dd-mon-yy.
Which one is it? VDATU can not be valid for both.

You're comparing a DATE with a String, try instead:
To_Date(VDATU,'DD-MON-YYYY') >= To_Date('03-JUN-2012','DD-MON-YYYY')
AND To_Date(VDATU,'dd-mon-yy') <= To_Date('04-SEP-2012','DD-MON-YYYY')
In case VDATU is already a date, you shouldn't convert it to_date, all you have to do is:
VDATU >= To_Date('03-JUN-2012','DD-MON-YYYY')
AND VDATU <= To_Date('04-SEP-2012','DD-MON-YYYY')

Related

Whats the correct way to use a date parameter in WHERE clause

Whats the correct way to use a parameter for
WHERE WORKS_ORDER_HEADER.DATEREQ < TO_CHAR('DD/MM/YYYY', '&CUT_OFF_DATE')
The data is presented in 16/06/1995 format
So if the parameter is less than that date, show all prior dates
WHERE date_collumn <= TO_DATE(:PARATEMER, 'DD/MM/YYYY')
Why would you try and compare CHAR when you could compare dates.
I think, you are looking for:
WHERE WORKS_ORDER_HEADER.DATEREQ < TO_DATE('&CUT_OFF_DATE', 'DD/MM/YYYY')
This will of course work only, if your DATEREQ is of type DATE.

Date format in Oracle- fetching Date of certain range

I have a date table in my db in Oracle. When I run a query I get the date format as '01-05-2015' but when I run a similar query in BIRT, I get the date format as '01-MAY-2015 12:00 AM'. How can I get the date format in dd/mm/yyy by keeping the data type of date field as date.
here is sample of my database.
EQ_DT
05-07-2015
06-06-2015
15-02-2015
19-09-2015
28-12-2015
also my query is :
select to_date(to_char(to_date(enquiry_dt,'DD/MM/YYYY'),'DD/MM/YY'),'DD/MM/YY') as q from xxcus.XXACL_SALES_ENQ_DATAMART where to_date(to_char(to_date(enquiry_dt,'DD/MM/YY'),'DD/MM/YY'),'DD/MM/YY')>'21-06-2012' order by q
I am getting error of NOT A VALID Month also
If enquiry_dt is already a date column, why are you trying to convert it to date (and then to char and to date again)?
SELECT to_char(enquiry_dt, 'DD/MM/YYYY') AS q
FROM xxcus.xxacl_sales_enq_datamart
WHERE enquiry_dt > to_date('21-06-2012', 'dd-mm-yyyy')
ORDER BY enquiry_dt
In birt, where you place the field on the report, set the field type to date. Then in properties for that field , go to format date time, and finally specify the date formatting you want for that field .
I prefer to always use pass date parameters as strings to BIRT, using a known date format. This is for report parameters as well as for DataSet parameters.
Then, inside the query, I convert to date like this:
with params as
( select to_date(pi_start_date_str, 'DD.MM.YYYY') as start_date_incl,
to_date(pi_end_date_str, 'DD.MM.YYYY') + 1 as end_date_excl
from dual
)
select whatever
from my_table, params
where ( my_table.event_date >= params.start_date_incl
and
my_table.end_date < params.start_date_excl
)
This works independent of the time of day.
This way, e.g. to select all events for january 2016, I could pass the query parameters '01.01.2016' and '31.01.2016' (I'm using german date format here).

I need fields of table between specific dates in oracle

I have table 'PORT_DTLS' with following fields: TN, Status_id, Req_Start_Time. Req_Start_Time has the time in this below format '8/12/2017 2:22:14.490361 PM'.
I have tried with this query..
select TN,Status_id
from PORT_DTLS
where Req_Start_Time between '8/11/2017 2:22:14.490361 PM'
and '8/12/2017 2:22:14.490361 PM';
But I am getting 'not a valid month' error.
Anyone please help me.
Your data looks like the column is a TIMESTAMP not a DATE. You should use an explicit formatting to cast those strings appropriately.
select TN,Status_id
from PORT_DTLS
where Req_Start_Time between to_timestamp('8/11/2017 14:22:14.490361' , 'DD/MM/YYYY HH24:MI:SS.FF')
and to_timestamp('8/12/2017 14:22:14.490361','DD/MM/YYYY HH24:MI:SS.FF');
I've assumed your dates are DD/MM; perhaps they are really MM/DD, in which case you need to tweak the format masks I posted. Most likely that ambiguity is why you're getting that ORA-01843 error.

compare 13digit (millisecond) unix timestamp with date in oracle

A database column (VARCHAR2 datatype) stores the date/time as 13 digit (milliseconds
) unixtimestamp format. Now when I want to compare the column with a oracle date (in question), The error thrown as 'invalid number'
I tried both ways,
converting the 13digit number to Date and compare with the date in question like below. The expressions seems valid as they are printed in select query, but if i include in the where part, it throws 'invalid number'
Here 'value' is 13th digit unixtimestamp column of VARCHAR2 datatype.
select
TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000,
TO_DATE('2014-04-21', 'YYYY-MM-DD')
from dummytable
-- where and TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000 > TO_DATE('2014-04-21', 'YYYY-MM-DD')
converting the date in question to 13digit unixtimestamp and comparing with the database column.The expressions seems valid as they are printed in select query, but if i include in the where part, it throws 'invalid number'
.
select
value,
(to_date('2013-04-21', 'YYYY-MM-DD') - to_date('1970-01-01', 'YYYY-MM-DD')) * (1000*24*60*60)
from dummytable
-- where value > ((to_date('2013-04-21', 'YYYY-MM-DD') - to_date('1970-01-01', 'YYYY-MM-DD')) * (1000*24*60*60))
any pointers? Thanks in advance.
[EDIT- 1 day later] I see the problem now. There are some data (other rows) for the 'value' column that are non-numeric. But I have another column say field, where always field='date' return value as 13 digit timestamp. Now I think when 'where' condition executes, although the field='date' is in the condition, it is still validating the other values for 'value' which are non-numeric. Is there a way to avoid this ?
Your code works just fine. The problem is in your data. Some of your values is not a number.
create table test
(value varchar2(13));
insert into test(value) values('2154534689000');
--insert into test(value) values('2 54534689000');
select TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000
from test
where TO_DATE('1970-01-01', 'YYYY-MM-DD') + value/86400000 > TO_DATE('2014-04-21', 'YYYY-MM-DD');
This code works fine. But if you uncommented the second insert, you would get exactly the same invalid number error as you get.
UPD. Allan gave you a nice hint, but i feel that it can be good to explain you a bit about views. The fact that you select from a view CAN make a difference. A view is not stored somewhere physically, when you select from it, it is just "added to your query". And then Oracle Query Optimizer starts working. Among other things, it can change the order in which your where predicates are evaluated.
For example, your the view query can have a line where value is not null and it would normally show only 'good' values. But if your query has a predicate where to_date(value,'ddmmyyyy') > sysdate, Oracle can decide to evaluate your predicate earlier, because Oracle predicts that it would "cut off" more rows, thus making the whole query faster and less momery consuming. Of course, execution will crash because of an attempt to convert a null string to date.
I believe, that Allan in his answer that he gave a link to, gave a great way to solve this problem: "wrapping" your query in a subquery that Oracle can't "unwrap":
select value
from
(select value
from my_view
where rownum > 0)
where to_date(value,'ddmmyyyy') > sysdate
Hope that helps.

Dynamic order by date data type in Oracle using CASE

My code in the stored procedure:
SELECT * FROM
my_table ir
WHERE
--where clause goes here
ORDER BY
CASE WHEN p_order_by_field='Id' AND p_sort_order='ASC' THEN IR.ID end,
CASE WHEN p_order_by_field='Id' AND p_sort_order='DESC' THEN IR.ID end DESC,
CASE WHEN p_order_by_field='Date' AND p_sort_order='ASC' THEN TO_CHAR(IR.IDATE, 'MM/dd/yyyy') end,
CASE WHEN p_order_by_field='Date' AND p_sort_order='DESC' THEN TO_CHAR(IR.IDATE, 'MM/dd/yyyy') end DESC;
Problem is that sorting is done based on the char, which comes out wrong for the date case. CASE statement, however, won't allow any other datatype other than char. So what is the solution in this case? I need to be able to pass the p_order_by_field into the stored procedure.
Thanks
Should be simple - just use ISO date format in your case:
TO_CHAR(IR.IDATE, 'yyyy-mm-dd')
and you should be fine.
Another problem could occure when you want to sort on the date difference (let say number of days between two days).
For example such a sort would return number 13 (days) before 9 (days).
The solution is that you concatenate length of date difference and the difference itself:
length(trunc(date2) - trunc(date1)) || to_char(date2 - date1)

Resources