简体   繁体   中英

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. 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' :

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

It depends entirely on what format your number start_date column is stored in.

However, it would probably be easier if you used the predicate on the true date column, and joined using a format mask just once.

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') . This query assumes that the a.start_date column is stored in the format 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.

Ps Why use a number to store a date?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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