SQL Server > Loops and Conditions

CASE to get searched condition in SQL Server

How to use CASE to check for searched condition in SQL Server?


In below query, we have used CASE statement to set the value of "Category" field based on Age column value.

SELECT FirstName, 'Category' =
CASE
WHEN Age <= 18 THEN 'Minor'
WHEN Age > 18 AND Age <= 30 THEN 'Major'
ELSE 'N/A'
END
FROM PersonalDetails

In above case, we are checking for value of Age column, WHEN Age <= 18 then setting ‘Category’ column value to ‘Minor’, when the age is in between 19 to 30 then setting ‘Category’ value to ‘Major’ else ‘N/A’.

 Views: 8280 | Post Order: 104



Write for us






Hosting Recommendations