Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 ORDER BY custom order

Author  Topic 

im1dermike
Posting Yak Master

222 Posts

Posted - 2010-01-11 : 15:09:33
I have a table that contains files for different customers. The types of files can be "CDC", "WSC", and "WAR". Any number of those files can exist meaning there could only be a "CDC" record for an agency, "CDC" and "WAR", or all three. I would like the result of my query to be ordered in the same order above ("CDC", "WSC", and "WAR"). Is there a way to add a custom order in an ORDER BY condition to accomplish what I'd like or should I do it after I get the query back (in C#)?

Thanks

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-11 : 15:10:50
Yes. You can use a CASE statement in your ORDER BY clause.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-11 : 15:15:07
Like..
select * from <urtable>
order by case file_type
when 'CDC' then 0
when 'WSC' then 1
when 'WAR' then 2
else 9 end
Go to Top of Page

im1dermike
Posting Yak Master

222 Posts

Posted - 2010-01-11 : 15:17:45
Seems obvious now, but I just wasn't thinking along those lines for whatever reason. Thanks so much vijay.
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2010-01-11 : 15:19:47
Vijay you are on fire today!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-11 : 15:21:53
quote:
Originally posted by im1dermike

Seems obvious now, but I just wasn't thinking along those lines for whatever reason. Thanks so much vijay.



Np. You're welcome.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-01-11 : 15:22:30
quote:
Originally posted by DP978

Vijay you are on fire today!



Yeah. Its that kind of day at work...Damn..I hate doing documentation. This is so much better
Go to Top of Page
   

- Advertisement -