Sql Server Computed Column Formula syntax
-
19-09-2019 - |
Question
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
Solution
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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow