How To Use a CASE Statement In T-SQL



TODO:

Have you ever wanted to use a CASE statement in your T-SQL Select Query?

 

SOLUTION:

SELECT   Column = 
      CASE 
         WHEN SomeValue =  0 THEN 'Zero'
         WHEN SomeValue < 0 THEN 'Minus'
         ELSE 'Plus'
      END
FROM MyTable

 

NOTES:

There are no notes on this topic.

How To Use A CASE Statement In A Select In T-SQL (SQL Server)



TODO:

Have you ever wanted to use a CASE statement in a SELECT statement in T-SQL?

 

SOLUTION:

SELECT  Id,
     CASE PrimaryColor WHEN NULL THEN 
          HeaderColor
     ELSE
          PrimaryColor
     END AS HeaderColor
     ,SectionHeader...
FROM...
WHERE...

 

NOTES:

This example will evaluate the column 'PrimaryColor'.  If it is NULL, then the column 'HeaderColor' will be used.  If it does have a value, then 'PrimaryColor' will be used.