Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server Computed Column Formula syntax

I want to use a computed bit column that will be true if another column in the table is not null. What's the correct formula for this?

HasLabel = computed column (bit)
Label = varchar NULL

The following formula does not validate. - what am I missing?
Formula for HasLabel = Label IS NOT NULL

like image 903
Jeremy Avatar asked Sep 30 '09 14:09

Jeremy


1 Answers

A computed column must return a value, whereas you are just doing a comparison. Try this instead:

case when label is null then 0 else 1 end

SQL Server will not understand this as a non-NULLable column however. To handle that, change the computation to:

isnull(case when label IS NULL then 0 else 1 end, 0)
like image 78
D'Arcy Rittich Avatar answered Sep 20 '22 23:09

D'Arcy Rittich