Click to See Complete Forum and Search --> : using case statement within a sql query


arch_mys
October 19th, 2005, 04:39 PM
Hi,
Can anybody guide me on how to use case and if statements within a sql query?

Thanks in advance

erickwidya
October 19th, 2005, 11:22 PM
from BOL
USE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO