SQL Server Inline CASE WHEN ISNULL and a Few Checks

I have a column with some zeros. If this column is NULL, I want to set the output for it based on the values ​​in another column.

So if case when null (if c=80 then 'planb'; else if c=90 then 'planc')

How would you code this in the T-SQL built-in statement?

thank.

+5
source share
2 answers
COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)
+12
source

You can also use the nested case statement. Assuming the first column is called a DataColumn.

CASE 
  WHEN DataColumn IS NULL THEN 
    CASE c 
      WHEN 80 THEN 'planb' 
      WHEN 90 THEN 'planc' 
      ELSE 'no plan' 
    END 
  ELSE DataColumn 
END
+4
source

All Articles