I have a column with some nulls. If that column is null, I want to condition output for it based on values in another column.
So if case when null (if c=80 then 'planb'; else if c=90 then 'planc')
How would you code that in an inline T-SQL statement?
thanks.
You can evaluate multiple conditions in the CASE statement.
advantage that COALESCE has over ISNULL is that it supports more than two inputs, whereas ISNULL supports only two. Another advantage of COALESCE is that it's a standard function (namely, defined by the ISO/ANSI SQL standards), whereas ISNULL is T-SQL–specific.
Comparing COALESCE and ISNULL Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence.
COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)
You can also use the nested case statement. Assuming that the first column is called DataColumn.
CASE
WHEN DataColumn IS NULL THEN
CASE c
WHEN 80 THEN 'planb'
WHEN 90 THEN 'planc'
ELSE 'no plan'
END
ELSE DataColumn
END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With