Solved

How to properly sort a varchar column in numeric order

Posted on 2012-03-11
4
435 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now