Case when else : Case « Query « SQL Server / T-SQL Tutorial






7> CREATE TABLE Product(
8>     ProductID               int                NOT NULL,
9>     Name                    nvarchar(25)       NOT NULL,
10>     ProductNumber           nvarchar(25)               ,
11>     Color                   nvarchar(15)       NULL,
12>      StandardCost            money              NOT NULL,
13>      Size                    nvarchar(5)        NULL,
14>      Weight                  decimal(8, 2)      NULL,
15>      ProductLine             nchar(20)           NULL,
16>      SellStartDate           datetime           NOT NULL,
17>      SellEndDate             datetime           NULL
18>  )
19>  GO
1> insert into Product values(1,'Product A', '1','Red',123.123,'1',1,'ProductLine A','1999-03-22','2000-03-22');
2> GO

(1 rows affected)
1>
2>
3> insert into Product values(2,'Product B', '2','Yellow',234.234,'1',3,'ProductLine B','2000-03-22','2001-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(3,'Product C', '3','Pink',345.345,'1',3,'ProductLine V','2001-09-22','2006-02-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(4,'Product D', '4','White',456.456,'1',4,'ProductLine D','2002-08-22','2006-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(5,'Product E', '5','Black',567.567,'1',5,'ProductLine E','2003-01-22','2003-04-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(6,'Product F', '6','Blue',678.678,'1',6,'ProductLine W','2004-02-22','2005-05-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(7,'Product G', '7','Drak',789.789,'1',7,'ProductLine Q','2005-03-22','2006-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(8,'Product H', '8','Gray',234.123,'1',8,'ProductLine F','2006-04-22','2006-09-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(9,'Product I', '9','Red',543.123,'1',9,'ProductLine R','2007-05-22','2008-03-22');
4> GO

(1 rows affected)
1>
2>
3> insert into Product values(0,'Product J', '0','Gold',765.123,'1',0,'ProductLine J','2008-06-22','2009-03-22');
4> GO

(1 rows affected)
1>
2>
3>
4>
5>
6> SELECT ProductID
7>  , Name
8>  , SubCategory = CASE ProductID
9>    WHEN 1 THEN 'Mountain Bike'
10>    WHEN 2 THEN 'Road Bike'
11>     WHEN 3 THEN 'Touring Bike'
12>     WHEN Null THEN 'Something Else'
13>     ELSE '(No Subcategory)'
14>   END
15>  FROM Product
16>  GO
ProductID   Name                      SubCategory
----------- ------------------------- ----------------
          1 Product A                 Mountain Bike
          2 Product B                 Road Bike
          3 Product C                 Touring Bike
          4 Product D                 (No Subcategory)
          5 Product E                 (No Subcategory)
          6 Product F                 (No Subcategory)
          7 Product G                 (No Subcategory)
          8 Product H                 (No Subcategory)
          9 Product I                 (No Subcategory)
          0 Product J                 (No Subcategory)

(10 rows affected)
1>
2>
3> drop table Product;
4> GO








1.23.Case
1.23.1.The syntax pattern for case
1.23.2.The syntax of the searched CASE function
1.23.3.A Simple CASE
1.23.4.CASE Expression
1.23.5.Automatic Code Generation for CASE Expressions
1.23.6.Summarizing Data Using the CASE Expression
1.23.7.Using a CASE expression to sum sales by weekday.
1.23.8.Using the CASE Expression with Complex Conditions
1.23.9.Summarizing Data Using the Searched CASE Expression
1.23.10.A SELECT statement that uses a simple CASE function
1.23.11.A SELECT statement that uses a searchable CASE function
1.23.12.Searched CASE expression looks for the first expression that evaluates to true.
1.23.13.Update statement based on case condition
1.23.14.Case when else
1.23.15.Alias for case statement
1.23.16.Use two case statements in one select statement
1.23.17.Case then with query
1.23.18.Case with range
1.23.19.Doing a calculation in a Searched CASE