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
 General SQL Server Forums
 New to SQL Server Programming
 Order By

Author  Topic 

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2012-01-17 : 09:40:55

Hi,

I have a field called 'title' in tbltest.
It has 5 values Mr, Mrs, Proffessor, Dr, Other.

I have a select query as

select * from dbo.tbltest
order by title


Results

Dr
Mr
Mrs
Other
Proffessor

I want it to be;

Dr
Mr
Mrs
Proffessor
Other


Intentionally, keep the 'Other' as the last data value and arranging the other values ascending order..

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-01-17 : 09:45:45
order by case when title='Other' then 1 else 0 end, title


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-17 : 10:16:15
OR...and I like this better

Add a column to the table called TitleOrder and place the values in the order you want them

Use Numbers like, 10, 20, 30, 40...so you have room to insert new titles on the fly



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -