You can use the CASE expression in a T-SQL query in SQL Server to get the same behavior of the switch statement in the programming languages.
Below is a sample Query demonstrating the implementation of the CASE expression in a query in SQL Server.
CASE expression in a Query in SQL Server
use AdventureWorks2014 GO SELECT Name, GroupName, CASE GroupName WHEN 'Research and Development' THEN 'United States of America' WHEN 'Quality Assurance' THEN 'United Kingdom' ELSE 'India' END AS Country FROM HumanResources.Department
The query associates a Department group name to a country USA and UK for Research and Development and Quality Assurance. For other departments , it associates the country India.
Leave a Reply