Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need to order combination of numbers and text

Author  Topic 

hztm2
Starting Member

16 Posts

Posted - 2010-05-20 : 10:49:04
Is there any way I can do something in a select query to get a mixture of numbers and text to order with the the numbers in numeric order. Suppose I have a table with a single varchar field RefNo

RefNo
100
120
N123456
1002

SELECT RefNo FROM Table ORDER BY RefNo produces
100
1002
120
N123456

this is exactly what I would expect but is there any way I can get the query to order the data in numeric order as below:
100
120
1002
N123456

Thank you

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2010-05-20 : 10:55:08
Try something like this:
order by right(replicate(' ',30)+RefNo,30)


CODO ERGO SUM
Go to Top of Page

hztm2
Starting Member

16 Posts

Posted - 2010-05-20 : 11:14:54
Thank you very much indeed - that does exactly what I needed - I guess I have a lot to learn!

Many thanks again
Go to Top of Page
   

- Advertisement -