Updated March 16, 2023
Introduction to T-SQL CASE
The T-SQL CASE is defined as the CASE that can be utilized with a statement or clause that can accept logical expressions; we can able to use the CASE in statements like SELECT, INSERT, UPDATE, DELETE, IN, and with clauses WHERE, ORDER BY, IN, and HAVING, in which CASE is the statement in T-SQL which can accept the conditions. It finds searches each row for the conditional statement; if the condition is fulfilled, then it gives back a value. If it does not have conditions, it returns the value in another block, which is not compulsory, and for false conditions.
What is T-SQL CASE?
The CASE is the statement in the T-SQL that can accomplish the conditions and give back the values; it allows us to put conditions in if-then-else statements; in the case statement, we can able to put the logical conditions with the result, and these statements always end with the ‘END’ statement, in which if we provide any condition in the case statement then it gives back the value in output if the given input get true then it will stop reading the sentences.
T-SQL CASE statement
The CASE statement can give back a value on particular conditions; the CASE statement can be used with the clauses such as Order By, Where, Group By; it accepts the logical conditions, and it provides the output as a value when the condition is true, and if not true then it goes in else block.
The CASE statement can provide the result by using specific values and conditions as per the logic, for example,
If we put the condition for admission in the 1st standard, then the logic will depend on the DOB column.
If the student’s age exceeds six years, the student will be eligible for the 1st standard.
T-SQL CASE Examples
Different examples are mentioned below
- Utilizing a SELECT statement with easy CASE expression:
A simple CASE expression can recognize only the impartial checks inside the SELECT statement, and no additional comparisons can be checked.
USE AdventureWork2013; GO SELECT ProductNumber, Category= CASE ProductLine WHEN 'A' THEN 'Apples' WHEN 'B' THEN 'Ball' WHEN 'C' THEN 'Cat' WHEN 'D' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber; GO
- Utilizing SELECT statement with a searched CASE expression:
Inside the SELECT statement, the searched CASE expression can be used for putting back the values in the result set.
USE AdventureWork2013; GO SELECT ProductNum, Name, "Price Range" = CASE WHEN ListPrice = 0 THEN 'Mfg item-not for resale' WHEN ListPrice < 60 THEN 'Under $60' WHEN ListPrice >= 60 and ListPrice < 350 THEN 'Under $350' WHEN ListPrice >= 350 and ListPrice < 2000 THEN 'Under $2000' ELSE 'Over $2000' END FROM Production.Product ORDER BY ProductNum; GO
- CASE statement in the Order By clause:
- The value of the ‘SalaryFlag’ column of the ‘HumanR’ table has been assessed, in which the employee who has ‘SalaryFlag’ is 1, then it gives back the Order by the ‘BusinessID’ in decreasing Order, and if the ‘SalaryFlag’ is zero, then it gives back the ‘BusinessID’ in increasing Order.
SELECT BusinessID, SalaryFlag FROM HumanR.Employee ORDER BY CASE SalaryFlag WHEN 1 THEN BusinessID END DESC ,CASE WHEN SalaryFlag = 0 THEN BusinessID END; GO
- In this example, the output is ordered by the column ‘Territory Name’ if the column ‘Country Name’ is the ‘United Kingdom and by ‘Country OrgName’ for all remaining rows.
SELECT BusinessID, LName, TerritoryName, CountryRegName FROM Sales.vSalesPerson WHERE TerritoryName IS NOT NULL ORDER BY CASE CountryRegName WHEN 'United States' THEN TerritoryName ELSE CountryRegName END;
- CASE in an UPDATE statement:
When we try to deduct ten from ‘HolidayHours,’ we will get the result as a negative value, and 40hrs have increased the ‘HolidayHours,’ or else ‘HolidayHours’ can be incremented by 20hrs.
USE AdventureWork2013; GO UPDATE HumanR.Employee SET HolidayHours = (CASE WHEN ((HolidayHours - 10.00) < 0) THEN HolidayHours + 40 ELSE (HolidayHours + 20.00) END ) OUTPUT Deleted.BusinessID, Deleted.HolidayHours AS BeforeValue, Inserted.HolidayHours AS AfterValue WHERE SalaryFlag = 0;
- Utilizing CASE in a SET statement:
In the below example, a SET statement has been used, the database function as ‘dbo.GetContactInfo’, which may give back the FName and LName for ‘BusinessID.’
USE AdventureWork2013; GO CREATE FUNCTION dbo.GetContactInformation(@BusinessID INT) RETURNS @retContactInformation TABLE ( BusinessID INT NOT NULL, FName NVARCHAR(50) NULL, LName NVARCHAR(50) NULL, ContactType NVARCHAR(50) NULL, PRIMARY KEY CLUSTERED (BusinessID ASC) ) AS BEGIN DECLARE @FName NVARCHAR(50), @LName NVARCHAR(50), @ContactType NVARCHAR(50); -- Get common contact information SELECT @BusinessID = BusinessID, @FName = FName, @LName = LName FROM Person.Person WHERE BusinessID = @BusinessID; SET @ContactType = CASE WHEN EXISTS(SELECT * FROM HumanR.Employee AS e WHERE e.BusinessID = @BusinessID) THEN 'Employee' WHEN EXISTS(SELECT * FROM Person.BusinessContact AS bec WHERE bec.BusinessID = @BusinessID) THEN 'Vendor' WHEN EXISTS(SELECT * FROM Purchasing.Vendor AS v WHERE v.BusinessID = @BusinessID) THEN 'Store Contact' WHEN EXISTS(SELECT * FROM Sales.Customer AS c WHERE c.PersonID = @BusinessID) THEN 'Consumer' END; IF @BusinessID IS NOT NULL BEGIN INSERT @retContactInformation SELECT @BusinessID, @FName, @LName, @ContactType; END; RETURN; END; GO SELECT BusinessID, FName, LName, ContactType FROM dbo.GetContactInformation(2200); GO SELECT BusinessID, FName, LName, ContactType FROM dbo.GetContactInformation(5);
- CASE in a HAVING clause:
In this example, the HAVING clause can limit the rows that give back the SELECT statement; it can give back the maximum hourly rate for every job title in the ‘HumanR’ table.
USE AdventureWork2013; GO SELECT JobTitle, MAX(ph1.Rate)AS MaxRate FROM HumanR.Employee AS e JOIN HumanR.EmployeePayHistory AS ph1 ON e.BusinessID = ph1.BusinessID GROUP BY JobTitle HAVING (MAX(CASE WHEN Gender = 'M' THEN ph1.Rate ELSE NULL END) > 40.00 OR MAX(CASE WHEN Gender = 'F' THEN ph1.Rate ELSE NULL END) > 42.00) ORDER BY MaxRate DESC;
In this article, we conclude that the CASE is the statement that has been used in the T-SQL, and it gains the conditional logic in an if-then-else statement and gives back the values which we require; we have also seen the T-SQL case statement and T-SQL case examples by using various statements.
This is a guide to T-SQL CASE. Here we discuss the Introduction, What T-SQL CASE is, and the T-SQL CASE statement with examples and code implementation. You may also have a look at the following articles to learn more –