将varchar日期与sysdate进行比较

时间:2014-09-18 20:32:03

标签: sql oracle to-date sysdate

我正在尝试查询sum_date WHERE sum_date BETWEEN (sysdate-1) AND (sysdate-30)

在我的SELECT声明中,我尝试了TO_DATE(sum_date, 'DD-MM-YY')sysdate

但它会引发错误:

  

"literal does not match format string."

  • Sum_date存储为varchar20,看起来像2014-01-07
  • sysdate看起来像18-SEP-14

2 个答案:

答案 0 :(得分:3)

您的格式为'YYYY-MM-DD',因此:

SELECT *
FROM   my_table
WHERE  TO_DATE(sum_date, 'YYYY-MM-DD') 
       BETWEEN (sysdate - 30) AND (sysdate - 1)

答案 1 :(得分:0)

您还可以更改会话的NLS_DATE_FORMAT以符合您的需求:

 
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' ;