Solved

How to properly sort a varchar column in numeric order

Posted on 2012-03-11
4
439 Views
Last Modified: 2012-03-12
I've been looking for a good solution for the last couple of hours - gotten close but most of what I've seen uses a case to sort integers first and then the varchars or similar. But none actually sort like I want and the things I have seen that purport to work are for Oracle and I'm using MSSQL.

That said, most of my data is int but there are a few rows that have either an 'x' or 'r' or 'c' which causes all sorts of issues with the sort.  a normal sort would return:
1372X
137X
1675X
19412
338X
3508
3543
3549

But, in an ideal world, I would like it to sort numerically - more like:
137X
338X
1372X
1675X
3508
3543
3549
19412

So, I would appreciate some help with this.  The client (or clients in this case) are very insistent that the sort look right - letters or no.  Many thanks in advance as always.
0
Comment
Question by:saabStory
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 37708443
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 37708560
try this
declare @T table(id varchar(20))

insert into @T
select '1372X'
union select '137X'
union select '1675X'
union select '19412'
union select '338X'
union select '3508'
union select '3543'
union select '3549'

select *, PATINDEX('%[A-Z]%', id) from @T 
order by case 
		when id is null then
			0
		when PATINDEX('%[A-Z]%', id) > 0 then	
			CAST(LEFT(id, PATINDEX('%[A-Z]%', id)-1) AS int )
		else 
			CAST (id as int)
		end

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 37708565
Here is a cleanedup version

select * 
from YourTable 
order by case 
			when YourField is null then
				0
			when PATINDEX('%[A-Z]%', YourField) > 0 then	
				CAST(LEFT(YourField, PATINDEX('%[A-Z]%', YourField)-1) AS int )
			else 
				CAST (YourField as int)
		end

Open in new window

0
 

Author Closing Comment

by:saabStory
ID: 37709784
Perfect!  Many, many thanks.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS Subscription jobs disabled, yet still running 4 50
SQL Server tables join on parse list 6 34
migrate a SQL 2008 to 2016, 2 34
Query Syntax 17 43
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question