0

I am trying to query sum_date WHERE sum_date BETWEEN (sysdate-1) AND (sysdate-30)

In my SELECT statement I have tried TO_DATE(sum_date, 'DD-MM-YY') and sysdate

But it throws error:

"literal does not match format string."

  • Sum_date is stored as varchar20 and looks like 2014-01-07.
  • sysdate looks like 18-SEP-14.
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
tx_query
  • 21
  • 2
  • 4

2 Answers2

3

Your format is 'YYYY-MM-DD', so:

SELECT *
FROM   my_table
WHERE  TO_DATE(sum_date, 'YYYY-MM-DD') 
       BETWEEN (sysdate - 30) AND (sysdate - 1)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You can also change the NLS_DATE_FORMAT of your session to match your need:

alter session set NLS_DATE_FORMAT = 'YYYY-MM-DD';

create table y 
  ( y varchar2(10));  
  select y.*, rowid from y
;

select * from y where y between sysdate -10000 and sysdate +100000;
select * from y where to_date(y,'YYYY-MM-DD') between '2010-04-01' and '2016-04-01' ;
J. Chomel
  • 8,193
  • 15
  • 41
  • 69