MSSQL - ORDER BY with a specific words
In SQL,the only option for Order by is either using Ascending or Descending. Sometimes,these two option is not possible to order the item based on your needs.
For example,let say you have a data like below and want to order by Type - Open then Close then Suspended.
Problem Type --------- -------- Problem 1 Suspended Problem 2 Suspended Problem 3 Open Problem 4 Open Problem 5 Close Problem 6 Close
You cannot get the expected result based on normal Ascending or Descending. Then, how you can solve this problem using a normal SQL query ? Well, the solution is pretty much easy actually.
You just need to modify a litte bit your order by statement to get the expected. As for this case, you can solve using below query.

select * from #XX order by replace(Type,’Open’,'A’)
So the trick is to replace your value that you want to make it appear at the top. In this case,’Open’ should be at the top,so we replace it with ‘A’ . Because A should be at the first position for asc order.
Hope it solve some of your SQL problems.
Till then…happy SQL’ING.
If you're new here, you may want to subscribe to my RSS feed or get my latest post directly in your mailbox. Thanks for visiting !
![[Ask]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/ask.png)
![[Bloglines]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/bloglines.png)
![[del.icio.us]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/digg.png)
![[Furl]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/furl.png)
![[StumbleUpon]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://www.arejae.com/blog/wp-content/plugins/bookmarkify/technorati.png)


Does this work with MYSQL also?
http://www.fadzuli.com
Fadzuli’s last blog post..Follow ups
yes,this works also in MYSQL.
abe … wubi tu reliable ke? takut install2 kea jane OS kawe … haha
drizzt’s last blog post..Come & Lick
bgs ar blog demo nie. respek. sek kelate hebat!
@drizz: kawe sendiri tok rajin try lagi wubi tuh abey weh. tapi secara teori,guna wubi nie utk elakkan dari menjahanamkan OS kito.eheh…acu try teh abey,bereh kawe raso.
@thinker:blog demo pun bijok gok.teruja jugok kawe tengok.ehehe…
What if you want multiple replace functions in a simple SQL statement? I tried it in many different ways and each time it didn’t work? Do you have a suggestion?
Hi Steven, I’m not very sure what you mean by multiple replaces.Is it something like below.
select * from #XX order by
replace(replace(Type,’Open’,’A’),’Suspended’,'B’)
this will order by - Open then Suspended then Close.
Arejae,
Sorry coz my comment is nothing to do with your entry. Just to say you deserve my recognition for linking-up my blog (silly me ..just realised today). I’ll put your link on mine at some point, hopefully by next week. Have been quite busy at the moment as I need to move to other lab which took me more or less 8 hours yesterday.
Another thing, from Pak Zawi’s newest blog I noticed that you were in SIS 89-93. Then you must know Fakrul Anwar Razali dont you?
rauhunt’s last blog post..Barcelona menyerang St Andrews
Rauhunt, bereh..takdo hal. wah..8 jam dalam lab jer…study tok hengat tuh.ehehe…bereh..blajar molek2.
oo…kenal lar dengan fakrul tuh..demo nie adik dio ko ? ehee
bukann 8 jam dlm lab ..tp perjalane 8 jam dari scotland ke southeast england ..tukar lab. fakrul tu spupu kawe
rauhunt’s last blog post..Barcelona menyerang St Andrews
ooo….jauh kaklos tuh.
kawe lamo benar dok jupo fakrul tuh…