Avatar of anwarmir
anwarmir
 asked on

BitMask handling in SQL

Hi,
 I have three bit columns colA, colB and colC.  If colA and ColC are both updated and set to 1. How can I automatically update the third Column ColC to 1 via a SQL rule or constraint. Meaning Colc is only set to 1 if ColA and ColB are 1.
Is there a better way to hanlde this design?
Microsoft SQL Server

Avatar of undefined
Last Comment
Aneesh

8/22/2022 - Mon
Aneesh

you can change the datatypes  of the first two columns from bit to smallint and can chnage the last column as a computed column of these two.

declare @Tab table (i smallint default 0, j smallint default 0 , k as (i+j))

insert into @tab select 1,0
union all select 1,1

Select * from @tab
Aneesh

there won't be any space/allocation issue between bit and smallint
Leigh Purvis

As Aneesh says - though would that not be
k as (i&j)
to do a bitwise comparison and get 1 in column k (/ColC)?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Aneesh

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.