Posts

Showing posts from August, 2008

Custom sorting using t-sql

I had an requirement where rows in a table needs to be sorted in a custom order instead on the column of a table.

Here is how the data in the table looks like.
ex: Customer

ID Name Address
1 a
2 b
3 c
4 SystemAdmin
5 ProgramAdmin
6 d
7 e

This is how the results should look like after sorting

ex:
ID Name Address
4 SystemAdmin
5 ProgramAdmin
1 a
2 b
3 c
6 d
7 e

Select ID,Name,Address
From Customer
Order by Replace(Replace(Name,'SystemAdmin','_a'),'ProgramAdmin','_b') Asc

the above t-sql is going to yeild the results wanted, but might not be the best and optimized query in the world.