简体   繁体   中英

oracle datetime not comparing properly

I am doing comparison between 2dates 01/01/2016 21:01:00 and 31/12/2015 00:12:00 with this code the result should be 1 but it shows 0

select 
case when ('01/01/2016 21:01:00'  >= (to_char(trunc(sysdate,'YEAR')-1  ,'dd/mm/yyyy HH24:MM:SS') )
)
then 1 else 0 end as Result
FROM dual

Can someone tell me where did i do wrong?

You are comparing strings not dates:

select case
       when TO_DATE( '2016-01-01 21:01:00', 'YYYY-MM-DD HH24:MI_SS' )
            >= trunc(sysdate,'YEAR')-1
       then 1
       else 0
       end as Result
FROM   dual

How about just comparing the years?

select (case when substr('01/01/2016 21:01:00', 7, 4) >= to_char(sysdate - interval '1' year, 'YYYY')
             then 1 else 0
        end) as Result
FROM dual;

Your logic is a hodge-podge of date and character logic.

当to_char(TO_DATE('2016-01-01 21:01:00','YYYY-MM-DD HH24:MI_SS'),'yy')+ 0> = to_char(sysdate,'yy')-1时选择大小写然后1 else 0结尾为Result FROM dual;

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