RE: Case Statement is Order By Clause !!

RE: Case Statement is Order By Clause !!

 

  

You can put a case statement in the order by clause to determine what
records you want to come first and so on. Here's an example of how it would
look...

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
CASE type
WHEN 'popular_comp' THEN 1
WHEN 'mod_cook' THEN 2
WHEN 'business' THEN 3
WHEN 'psychology' THEN 4
WHEN 'trad_cook' THEN 5
ELSE 6
END

-----Original Message-----
From: Parag Kulkarni [mailto:[Email Address Removed]
Sent: Tuesday, September 17, 2002 3:27 PM
To: LazyDBA.com Discussion
Subject: Case Statement is Order By Clause !!

Hi All,


I need to order the resultset depending on certain
criteria and one of my collegues suggested to use a
case statement in Order By Clause.I checked the help
and couldnt find out how to use the case statement in
Order By clause.My main question is CAN I USE A CASE
STATEMENT in ORDER BY Clause ?

TIA,
Parag.


__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: mssqldba-[Email Address Removed] additional commands, e-mail: mssqldba-[Email Address Removed]MS Sql Server LazyDBA home page