CASE Statement in SQL - Quick Tip

CASE Statement in SQL - Quick Tip


CASE Statement in SQL - Quick Tip -DotNetKida



CASE

CASE statements are used to create different outputs (Usually in the SELECT statement).
It is SQL's way of handling if-then logic.

SELECT [COLUMN_NAME] ,
             
              CASE

                     WHEN [CONDITION 1] THEN 'OUTPUT 1'

                     WHEN [CONDITION 2] THEN 'OUTPUT 2'

                     ELSE 'OUTPUT 3'
             
              END

FROM [TABLE_NAME]



In the above query, the output will be based on the conditions, if CONDITION 1 is true then the output will be OUTPUT 1, likewise, if CONDITION 2 is true then the output will be OUTPUT 2, if both conditions are false then the output will be OUTPUT 3.

Post a Comment

0 Comments