将Oracle sql替换为postgres sql

vybvopom  于 2023-02-03  发布在  Oracle
关注(0)|答案(2)|浏览(136)

postgres sql中bitand函数替换和oracle函数解码

select decode(bitand(p.privilege, 2), 2, 'true', 'false') as is_approver 
from person p;

在sql下尝试

select case ((p.privilege & 2 = 2))::int when 2 then 'true' else 'false' end as is_approver  
from person p;
1tu0hz3e

1tu0hz3e1#

表达式p.privilege & 2 = 2将返回一个boolean值。如果您可以在应用程序中使用它们,那么您所需要的就是:

select (p.privilege::int & 2) = 2 as is_approver
from person p;

如果确实需要truefalse的字符串值,那么需要测试布尔值。

select case 
         when (p.privilege::int & 2) = 2 then 'true'
         else 'false'
       end as is_approver
from person p;
9jyewag0

9jyewag02#

那应该是

select (p.privilege::bigint & 2) = 2 as is_approver 
from person p;

相关问题