Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 multiple bit fields as one new bit field result

Author  Topic 

adiel
Starting Member

16 Posts

Posted - 2009-02-09 : 13:52:11
If you have two bit fields and would like to have a new field set to true(value of 1 for a sql server bit field) if either of the two bit fields are true(value of 1 for a sql server bit field), you can easily do something like this:

--Note: This is what I have to use now in T-SQL(Sql Server 2005)
select case
when bitfield1 = 0 and bitfield2 = 0 then 0
else 1
end as bitfieldBoolResult
from ...

My question is, is there an easier way to do this such as:

--Note: The below statement does not exist in T-SQL(Sql Server 2005)
select (bitfield1 or bitfield2) as bitfieldBoolResult
from ...

Thanks Before Hand,
Adiel

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-02-09 : 13:57:09
bitwise OR operator in Sql is the <pipe>:

bitfield1 | bitfield2

Be One with the Optimizer
TG
Go to Top of Page

adiel
Starting Member

16 Posts

Posted - 2009-02-09 : 14:06:55
quote:
Originally posted by TG

bitwise OR operator in Sql is the <pipe>:

bitfield1 | bitfield2

Be One with the Optimizer
TG



Thank you sir that worked.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-02-09 : 14:20:21
You're welcome

Are you sure you need to store it since it can be derived by the other 2 columns?

Be One with the Optimizer
TG
Go to Top of Page

adiel
Starting Member

16 Posts

Posted - 2009-02-13 : 12:41:17
quote:
Originally posted by TG

You're welcome

Are you sure you need to store it since it can be derived by the other 2 columns?

Be One with the Optimizer
TG



I was actually using it for a select statement on a cursor which would create a temp field I was later going to use inside the cursor loop to determine if I should insert a record in another table. It worked out great.

Thanks Again,
Adiel
Go to Top of Page
   

- Advertisement -