简体   繁体   English

MySQL查询到Oracle查询的转换

[英]MySQL Query to Oracle Query Conversion

Consider an sql query like 考虑像这样的SQL查询

SELECT a, b>now() as expired 
FROM TABLE;

where b is a date and checking with current date. 其中b是日期,并检查当前日期。

I found out that now() should be replaced with SYSDATE 我发现now()应该用SYSDATE代替

but how to write 但是怎么写

 b > (SYSDATE) as expired 

for Oracle? 对于Oracle?

Oracle does not have a boolean datatype (neither does MySQL but it simply treats any number not equal to zero as "true"), so you need return a number indicating the expiration as 0 and 1 Oracle没有布尔数据类型(MySQL也不是,但它只是将不等于零的任何数字都视为“ true”),因此您需要返回一个数字,表示到期时间为01

select a, 
       case 
          when b > sysdate then 1
          else 0
       end as expired
from the_table;

Note that Oracle's DATE datatype includes the time. 请注意,Oracle的DATE数据类型包括时间。 So SYSDATE returns something like 2013-04-04 14:43:12 . 因此SYSDATE返回类似2013-04-04 14:43:12 You probably want to use trunc() on the comparison: 您可能想在比较中使用trunc():

select a, 
       case 
          when trunc(b) > trunc(sysdate) then 1
          else 0
       end as expired
from the_table;

Of course you can return anything in the case statement, not only numbers 当然,您可以在case语句中返回任何内容,不仅是数字

select a, 
       case 
          when trunc(b) > trunc(sysdate) then 'expired'
          else 'active'
       end as expired
from the_table;

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

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