我正在查询数据库,我需要组合2位列(对于此示例,如果一个为真,则列必须为true)。
像:Select col1 || col2 from myTable
实现这一目标的最简单方法是什么?
select col1 | col2 from myTable
我假设col1和col2是位值,最接近的Sql Server必须是布尔值。
要返回1或0:
select case when col1=1 or col2=1 then 1 else 0 end
from yourtable
要返回true或false:
select case when col1=1 or col2=1 then 'true' else 'false' end
from yourtable
您想使用Bitwise操作
& - 所有条件都需要匹配
| - 任何条件都需要匹配
Select
-- Both need to Match
1 & 0, -- false
1 & 1, -- true
-- Only one condition needs to match
1 | 1, -- true
0 | 1, -- true
1 | 0, -- true
0 | 0 -- False