Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordering a Varchar Field... 1

Status
Not open for further replies.

Rebies

Programmer
Mar 7, 2002
58
US
I have a field called 'rating' which is of the datatype Varchar. I am trying to sort it, but am having two problems. One is that when I "order by rating desc" I find that a rating of 1 or 10 is listed last because thats how it is alphabetially sorted. When there is not a rating I have the content of 'N/A' in there, and the 'N/A's show up first because alphabetially they are ordered first. (when doing a DESC order by that is)

Unfortunately I can not change how this is set up, but need to get the ordering correct.

Any ideas? I'm mostly limited to changing the "ORDER BY" clause, and not much else.

Andrew
 
order by case
when isNumeric(rating) then cast(rating as int)
else -1 end desc
 
Wow, thats intuitive. I would have never thought of doing that. Thanks for the help. I was starting to think I was up some sort of creek.

:)

Andrew
 
Oh, just for your info (or anyone else that reads this)..

You do need to check isNumeric(rating) to something, so the acutal code is:

ORDER BY case
WHEN isNumeric(rating) = 1 THEN cast(rating as int)
ELSE -1 END DESC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top