In addition to renaming columns in a results set, you can also categorize or rename data in a results set. For example, the following query uses the CASE expression to rename book categories so that they are more understandable:
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, 'Shortened Title' = CONVERT(varchar(30), title), Price = price FROM titles WHERE price IS NOT NULL ORDER BY 1
The results look like this:
category shortened title Price ------------------- ------------------------------ ------- Business Cooking with Computers: Surrep 11.95 Business Straight Talk About Computers 19.99 Business The Busy Executive's Database 19.99 Business You Can Combat Computer Stress 2.99 Modern Cooking Silicon Valley Gastronomic Tre 19.99 Modern Cooking The Gourmet Microwave 2.99 Popular Computing But Is It User Friendly? 22.95 Popular Computing Secrets of Silicon Valley 20.00 Psychology Computer Phobic AND Non-Phobic 21.59 Psychology Emotional Security: A New Algo 7.99 Psychology Is Anger the Enemy? 10.95 Psychology Life Without Fear 7.00 Psychology Prolonged Data Deprivation: Fo 19.99 Traditional Cooking Fifty Years in Buckingham Pala 11.95 Traditional Cooking Onions, Leeks, and Garlic: Coo 20.95 Traditional Cooking Sushi, Anyone? 14.99 (16 row(s) affected)