如何组合2位列

问题描述 投票:11回答:3

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

像:Select col1 || col2 from myTable

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

sql sql-server tsql
3个回答
15
投票

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

0
投票

您想使用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
© www.soinside.com 2019 - 2024. All rights reserved.