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?
select col1 | col2 from myTable
http://msdn.microsoft.com/en-us/library/ms176122.aspx
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With