mcalex
mcalex

Reputation: 6778

Add computed boolean column SQL Server

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?

Upvotes: 5

Views: 3426

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93724

You misplaced the parenthesis. try this.

      Cast (CASE
               WHEN  [ColumnA] =  1  
                    AND  [ColumnB] =  0   THEN  1 
               ELSE  0 
             END AS BIT) 

Upvotes: 9

Related Questions