Skip to main content

CASE Statement with Example in SQL SERVER


The SQL CASE statement has WHEN, THEN, and ELSE clauses along with an END terminator.

Example

DECLARE @Gender VARCHAR(50)
SET @Gender='Male'

SELECT
CASE @Gender
          WHEN 'Male'   THEN 'M'
          WHEN 'Female' THEN 'F'
          ELSE  null
END       as 'Gender'

SQL Server compares this @Gender  to the expression  and when the values match, it returns the THEN clauses ‘M’or ‘F’. If none of the WHEN clauses match then it will return ‘null’ value

Comments