简体   繁体   English

比较日期列时的oracle sql查询错误

[英]oracle sql query error while comparing date columns

I have a following oracle sql query in which START_DATE is a number column and a_date is DATE type and the input value is also of type DATE. 我有以下oracle sql查询,其中START_DATE是数字列,而a_date是DATE类型,输入值也是DATE类型。 kindly let me know how to compare the date columns with the input date. 请让我知道如何将日期列与输入日期进行比较。

select a.id ,a.v ,b.id,b.v  
 from DATA a ,FDC  b  where a.START_DATE = to_date('11-DEC-10','YYYYMMDD')
 and a.START_DATE = b.a_date and b.code = 'JFK'
select a.id ,a.v ,b.id,b.v  
 from DATA a ,FDC  b  where a.START_DATE LIKE TO_DATE('11-DEC-10','DD-MON-YY')
 and a.START_DATE = b.a_date and b.code = 'JFK'

If you stored your START_DATE as Number like 'YYYYMMDD' : 如果您将START_DATE作为'YYYYMMDD''YYYYMMDD'数字存储:

a.START_DATE=TO_NUMBER(TO_DATE('20101211','YYYYMMDD'))

It depends entirely on what format your number start_date column is stored in. 这完全取决于您的数字start_date列存储的格式。

However, it would probably be easier if you used the predicate on the true date column, and joined using a format mask just once. 但是,如果在true date列上使用谓词,然后仅使用一次格式掩码加入谓词,则可能会更容易。

For example: 例如:

SELECT a.id,
       a.v, 
       b.id,
       b.v  
  FROM data a, 
       fdc b
 WHERE b.a_date = to_date('11-DEC-2010','DD-MON-RRRR')
   AND a.start_date = TO_NUMBER(TO_CHAR(b.a_date, 'DDMMRRRR'))
   AND b.code = 'JFK'

Please note that the date format matches the format of the date you are comparing - b.a_date = to_date('11-DEC-2010','DD-MON-RRRR') . 请注意,日期格式与您要比较的日期格式匹配b.a_date = to_date('11-DEC-2010','DD-MON-RRRR') This query assumes that the a.start_date column is stored in the format DDMMRRRR . 该查询假定a.start_date列以DDMMRRRR格式DDMMRRRR You would need to amend this for whichever format your date is stored in egastart_date = TO_NUMBER(TO_CHAR(b.a_date, 'J')) for a Julian date. 对于将日期存储在egastart_date = TO_NUMBER(TO_CHAR(b.a_date, 'J'))中的日期,无论哪种格式,您都需要对其进行修改。

Ps Why use a number to store a date? Ps为什么使用数字存储日期?

从tbltlcrconfighistory中选择*,其中TO_DATE(STARTDATE,'dd-mon-yyyy')= TO_DATE('14 -dec-2010','dd-mon-yyyy');

从tbltlcrconfighistory中选择*,其中STARTDATE = TO_DATE('2010年12月14日','dd-mon-yyyy');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM