5

How do I add a computed boolean (bit) column to a SQL Server table that depends on two other boolean columns? (I'd prefer a SSMS solution).

I can get a computed column with the correct values by using the following:

case when ([ColumnA]=(1)) AND ([ColumnB]=(0)) then (1) else (0) end

but then the resulting column is type int which falls over when I use the data in C#

Looking around, it seems I need to use a CAST statement but I haven't managed to work out the syntax. I know this doesn't work:

cast (case when ([ColumnA]=(1)) AND ([ColumnB]=(0)) then (1) else (0) end) as bit

What should I do?

1 Answer 1

9

You misplaced the parenthesis. try this.

      Cast (CASE
               WHEN  [ColumnA] =  1  
                    AND  [ColumnB] =  0   THEN  1 
               ELSE  0 
             END AS BIT) 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I thought only the case ... end was bracketed. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.