Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine 2 bit columns

I am querying a database and I have 2 bit columns I need to combine (for this example if one is true the column must be true).

Something like: Select col1 || col2 from myTable

What is the easiest way of achieving this?

like image 426
Sergio Avatar asked May 04 '09 10:05

Sergio


3 Answers

select col1 | col2 from myTable

http://msdn.microsoft.com/en-us/library/ms176122.aspx

like image 105
Stefan Steinegger Avatar answered Nov 19 '22 05:11

Stefan Steinegger


I'm assuming col1 and col2 are bit values, the closest Sql Server has to booleans.

To return 1 or 0:

select case when col1=1 or col2=1 then 1 else 0 end
from yourtable

To return true or false:

select case when col1=1 or col2=1 then 'true' else 'false' end
from yourtable
like image 4
Andomar Avatar answered Nov 19 '22 06:11

Andomar


You want to use Bitwsise operations

& - All conditions needs to match

| - Any condition needs to match

    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
like image 1
H20rider Avatar answered Nov 19 '22 06:11

H20rider