2014-09-18 107 views
0

我想查詢sum_date WHERE sum_date BETWEEN (sysdate-1) AND (sysdate-30)比較VARCHAR日期SYSDATE

在我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
+3

試試這個TO_DATE(sum_date,「YYYY -mm-DD') – Aramillo 2014-09-18 20:35:57

回答

3

你的格式是'YYYY-MM-DD',所以:

SELECT * 
FROM my_table 
WHERE TO_DATE(sum_date, 'YYYY-MM-DD') 
     BETWEEN (sysdate - 30) AND (sysdate - 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' ;