troubleshooting Question

Sorting strings & numbers in SQL query

Avatar of andrishelp
andrishelp asked on
Microsoft SQL Server 2005
10 Comments2 Solutions290 ViewsLast Modified:
TableName: cartrows
FieldName: sortorder and datatype : varchar
Data: 1, 5.23, ALL, S1, S2, S3, S4, 5.23, 11, ALL, S1, S2, S4.12, S5

When I write this query :
SELECT * FROM cartrows
case isnumeric(replace(sortorder,',','.')) when 1 then cast(replace(sortorder,',','.') as float)
else ASCII(replace(sortorder,',','.'))  end

Result I got  this : 1, 5.23, 5.23, 11, ALL, ALL, S1, S2, S3, S4.12, S5, S1, S2, S4

Expected result : 1, 5.23, 5.23, 11, ALL, ALL, S1, S1, S2, S2, S3, S4, S4.12, S5

Can anyone please help me with this sorting?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros