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)
 Sort alphanumeric varchar column

Author  Topic 

Mayiko
Starting Member

9 Posts

Posted - 2012-03-29 : 02:29:46
First off, let me say I am terrible at T-SQL.

Now, I need to sort a column that has a fixed length and a fixed schema for the data. The data in the column will always be in this form:

LL12-AA99999

The LL will have three possible values. These are WT, WB, and BR. The 12 is the year, and will increment as such. The AA00001 is an incrementing value that will rollover and become AB00000. When the second letter gets to Z it will rollover to BA00000.

So, if I have these values in my DB:

WT12-AA00001
WT12-AA00002
WT12-AA10001
WT12-AA10002
WT12-AB00001
WT12-BA00001

I need it to sort in reverse order (the opposite of what I listed). When I am selecting the data I am passing in a value to act as a filter, so the data I will be sorting will only be the part after the "-". Everything before the "-" will be the same.

The reason why I need to do this is these numbers are reference numbers, so I need to know what the largest number is so I can create the next one in the series.

Thanks for your help.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-29 : 07:21:38
I may not have quite followed what your requirements are but, would one of these sort them correctly for you?

SELECT * FROM YourTable ORDER BY Col1 DESC;
SELECT * FROM YourTable ORDER BY RIGHT(Col1,7) DESC;
Go to Top of Page
   

- Advertisement -