Case in Select Statement Case in Select Statement sql-server sql-server

Case in Select Statement


The MSDN is a good reference for these type of questions regarding syntax and usage. This is from the Transact SQL Reference - CASE page.

http://msdn.microsoft.com/en-us/library/ms181765.aspx

USE AdventureWorks2012;GOSELECT   ProductNumber, Name, "Price Range" =   CASE      WHEN ListPrice =  0 THEN 'Mfg item - not for resale'     WHEN ListPrice < 50 THEN 'Under $50'     WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'     WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'     ELSE 'Over $1000'  ENDFROM Production.ProductORDER BY ProductNumber ;GO

Another good site you may want to check out if you're using SQL Server is SQL Server Central. This has a large variety of resources available for whatever area of SQL Server you would like to learn.


I think these could be helpful for you .

Using a SELECT statement with a simple CASE expression

Within a SELECT statement, a simple CASE expression allows for only an equality check; no other comparisons are made. The following example uses the CASE expression to change the display of product line categories to make them more understandable.

USE AdventureWorks2012;GOSELECT   ProductNumber, Category =      CASE ProductLine         WHEN 'R' THEN 'Road'         WHEN 'M' THEN 'Mountain'         WHEN 'T' THEN 'Touring'         WHEN 'S' THEN 'Other sale items'         ELSE 'Not for sale'      END,   NameFROM Production.ProductORDER BY ProductNumber;GO

Using a SELECT statement with a searched CASE expression

Within a SELECT statement, the searched CASE expression allows for values to be replaced in the result set based on comparison values. The following example displays the list price as a text comment based on the price range for a product.

USE AdventureWorks2012;GOSELECT   ProductNumber, Name, "Price Range" =       CASE          WHEN ListPrice =  0 THEN 'Mfg item - not for resale'         WHEN ListPrice < 50 THEN 'Under $50'         WHEN ListPrice >= 50 and ListPrice < 250 THEN 'Under $250'         WHEN ListPrice >= 250 and ListPrice < 1000 THEN 'Under $1000'         ELSE 'Over $1000'      ENDFROM Production.ProductORDER BY ProductNumber ;GO

Using CASE in an ORDER BY clause

The following examples uses the CASE expression in an ORDER BY clause to determine the sort order of the rows based on a given column value. In the first example, the value in the SalariedFlag column of the HumanResources.Employee table is evaluated. Employees that have the SalariedFlag set to 1 are returned in order by the BusinessEntityID in descending order. Employees that have the SalariedFlag set to 0 are returned in order by the BusinessEntityID in ascending order. In the second example, the result set is ordered by the column TerritoryName when the column CountryRegionName is equal to 'United States' and by CountryRegionName for all other rows.

SELECT BusinessEntityID, SalariedFlagFROM HumanResources.EmployeeORDER BY CASE SalariedFlag WHEN 1 THEN BusinessEntityID END DESC        ,CASE WHEN SalariedFlag = 0 THEN BusinessEntityID END;GOSELECT BusinessEntityID, LastName, TerritoryName, CountryRegionNameFROM Sales.vSalesPersonWHERE TerritoryName IS NOT NULLORDER BY CASE CountryRegionName WHEN 'United States' THEN TerritoryName         ELSE CountryRegionName END;

Using CASE in an UPDATE statement

The following example uses the CASE expression in an UPDATE statement to determine the value that is set for the column VacationHours for employees with SalariedFlag set to 0. When subtracting 10 hours from VacationHours results in a negative value, VacationHours is increased by 40 hours; otherwise, VacationHours is increased by 20 hours. The OUTPUT clause is used to display the before and after vacation values.

USE AdventureWorks2012;GOUPDATE HumanResources.EmployeeSET VacationHours =     ( CASE         WHEN ((VacationHours - 10.00) < 0) THEN VacationHours + 40         ELSE (VacationHours + 20.00)       END    )OUTPUT Deleted.BusinessEntityID, Deleted.VacationHours AS BeforeValue,        Inserted.VacationHours AS AfterValueWHERE SalariedFlag = 0; 

Using CASE in a HAVING clause

The following example uses the CASE expression in a HAVING clause to restrict the rows returned by the SELECT statement. The statement returns the the maximum hourly rate for each job title in the HumanResources.Employee table. The HAVING clause restricts the titles to those that are held by men with a maximum pay rate greater than 40 dollars or women with a maximum pay rate greater than 42 dollars.

USE AdventureWorks2012;GOSELECT JobTitle, MAX(ph1.Rate)AS MaximumRateFROM HumanResources.Employee AS eJOIN HumanResources.EmployeePayHistory AS ph1 ON e.BusinessEntityID = ph1.BusinessEntityIDGROUP BY JobTitleHAVING (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 MaximumRate DESC;

For more details description of these example visit the source.

Also visit here and here for some examples with great details.


you can also use:

SELECT CASE         WHEN upper(t.name) like 'P%' THEN          'productive'         WHEN upper(t.name) like 'T%' THEN          'test'         WHEN upper(t.name) like 'D%' THEN          'development'         ELSE          'unknown'       END as typeFROM table t