CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
2.
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'
END
SELECT
Store_Name,
CASE Store_Name
WHEN 'Los Angeles' THEN Sales * 2
WHEN 'San Diego' THEN Sales * 1.5
ELSE Sales
END AS "New Sales",
Txn_Date
FROM Store_Information;
UPDATE table_name
SET column_name=CASE
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
WHEN column_name in ('value1', 'value2',.....)
THEN 'update_value'
END
--Simple CASE expression:
SELECT FirstName, State=(CASE StateCode
WHEN 'MP' THEN 'Madhya Pradesh'
WHEN 'UP' THEN 'Uttar Pradesh'
WHEN 'DL' THEN 'Delhi'
ELSE NULL
END), PayRate
FROM dbo.Customer
-- Searched CASE expression:
SELECT FirstName,State=(CASE
WHEN StateCode = 'MP' THEN 'Madhya Pradesh'
WHEN StateCode = 'UP' THEN 'Uttar Pradesh'
WHEN StateCode = 'DL' THEN 'Delhi'
ELSE NULL
END), PayRate
FROM dbo.Customer
2.用CASE表达式更新语句
-- Simple CASE expression:
UPDATE Customer
SET StateCode = CASE StateCode
WHEN 'MP' THEN 'Madhya Pradesh'
WHEN 'UP' THEN 'Uttar Pradesh'
WHEN 'DL' THEN 'Delhi'
ELSE NULL
END
-- Simple CASE expression:
UPDATE Customer
SET StateCode = CASE
WHEN StateCode = 'MP' THEN 'Madhya Pradesh'
WHEN StateCode = 'UP' THEN 'Uttar Pradesh'
WHEN StateCode = 'DL' THEN 'Delhi'
ELSE NULL
END
3.带有CASE表达式的ORDER BY子句
-- Simple CASE expression:
SELECT * FROM dbo.Customer
ORDER BY
CASE Gender WHEN 'M' THEN FirstName END Desc,
CASE Gender WHEN 'F' THEN LastName END ASC
-- Searched CASE expression:
SELECT * FROM dbo.Customer
ORDER BY
CASE WHEN Gender='M' THEN FirstName END Desc,
CASE WHEN Gender='F' THEN LastName END ASC
4.带有CASE表达的从句
-- Simple CASE expression:
SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
FROM dbo.Customer
GROUP BY StateCode,Gender,FirstName
HAVING (MAX(CASE Gender WHEN 'M'
THEN PayRate
ELSE NULL END) > 180.00
OR MAX(CASE Gender WHEN 'F'
THEN PayRate
ELSE NULL END) > 170.00)
-- Searched CASE expression:
SELECT FirstName ,StateCode,Gender, Total=MAX(PayRate)
FROM dbo.Customer
GROUP BY StateCode,Gender,FirstName
HAVING (MAX(CASE WHEN Gender = 'M'
THEN PayRate
ELSE NULL END) > 180.00
OR MAX(CASE WHEN Gender = 'F'
THEN PayRate
ELSE NULL END) > 170.00)
Select s.stock_code,s.stock_desc,s.stock_desc_ar,
mc.category_name,s.sel_price,
case when s.allow_discount=0 then 'Non Promotional Item' else 'Prmotional
item' end 'Promotion'
From tbl_stock s inner join tbl_stock_category c on s.stock_id=c.stock_id
inner join tbl_category mc on c.category_id=mc.category_id
where mc.category_id=2 and s.isSerialBased=0