2013-04-03 87 views
0

考虑SQL查询等MySQL查询Oracle查询转换

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

其中b是日期与当前日期检查。

我发现now()SYSDATE

但如何更换,以写

b > (SYSDATE) as expired 

为Oracle?

回答

3

Oracle没有一个布尔值数据类型(同样没有MySQL,但它只是将任意数量的不等于零为“真”),所以你需要返回一个数字,指示期满为01

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

请注意,Oracle的DATE数据类型包含时间。因此SYSDATE返回类似2013-04-04 14:43:12。你可能想在比较使用TRUNC():

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

当然你也可以在case语句返回任何东西,不仅数字

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

由于它的作品! –