简体   繁体   English

oracle datetime比较不正确

[英]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 我正在使用此代码在2dates 01/01/2016 21:01:00和31/12/2015 00:12:00之间进行比较,结果应为1但显示为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;

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

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