devxlogo

Sorting SQL Results in Your Own Preferred Order

Sorting SQL Results in Your Own Preferred Order

USE PubS-- Sort Each of PubName in Natural Alphabetic Order but push A's to thelastselect * from publishersOrder by (CASE left(Pub_name,1) WHEN 'A' THEN 1000 ELSE ASCII(pub_name)END)--Sort Publishers By City in your own Order of Preference first and in anatural order for the--remaining cities. Also remaning cities to be sorted in the reverse alphaorderselect * from publishersOrder by (CASE CityWHEN 'Paris' 	THEN 1WHEN 'Chicago' 	THEN 2WHEN 'Boston' 	THEN 3WHEN 'New York' THEN 4WHEN 'Berkeley' THEN 5WHEN 'Dallas' 	THEN 6ELSE 100 END) ASC,City DESC

devx-admin

Share the Post: