Hell.Bent
Hell.Bent

Reputation: 1677

SQL Server Inline CASE WHEN ISNULL and multiple checks

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.

Upvotes: 5

Views: 14960

Answers (2)

Leons
Leons

Reputation: 2674

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

Upvotes: 4

Martin Smith
Martin Smith

Reputation: 453608

COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)

Upvotes: 12

Related Questions