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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Properly sorting strings that contain numeric values 1

Status
Not open for further replies.

scoob28

Technical User
Feb 19, 2008
12
US
I have a list of items that are strings containing the 4 digit year followed by a space and then a number containing a decimal point. They currently sort like this.

1994 115.04
1994 119.4
1994 12.4
1994 121.03
1994 121.6
1994 124.01
1994 13.12

I'd like them to sort like this.

1994 12.4
1994 13.12
1994 115.04
1994 119.4
1994 121.03
1994 121.6
1994 124.01

Thanks!
 
Select *
from mytable
order by convert(decimal,Substring(myfield,7,999))
 
Thanks PWise. My example was not a good one. There are several different years and I need it to sort by the year also.
 
Select *
from mytable
order by
convert(int,left(myfield,4))
convert(decimal,Substring(myfield,7,999))
 
Thanks PWise. This is what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top