如何组合2位列

时间:2009-05-04 10:21:58

标签: sql sql-server tsql

我正在查询数据库,并且我需要组合2位列(对于此示例,如果一个为真,则列必须为true)。

类似于:Select col1 || col2 from myTable

实现这一目标的最简单方法是什么?

3 个答案:

答案 0 :(得分:15)

答案 1 :(得分:5)

我假设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

答案 2 :(得分:0)

您要使用Bitwsise操作

&-所有条件都必须匹配

| -任何条件都必须匹配

    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
相关问题