I am using SQL Server, the column is a VARCHAR(50)
and I want to sort it like this:
1A
1B
2
2
3
4A
4B
4C
5A
5B
5C
5N
14 Draft
21
22A
22B
23A
23B
23C
23D
23E
25
26
FR01584
MISC
What I have so far is:
Select *
From viewASD
ORDER BY
Case When IsNumeric(LEFT(asdNumNew,1)) = 1
Then CASE When IsNumeric(asdNumNew) = 1
Then Right(Replicate('0',20) + asdNumNew + '0', 20)
Else Right(Replicate('0',20) + asdNumNew, 20)
END
When IsNumeric(LEFT(asdNumNew,1)) = 0
Then Left(asdNumNew + Replicate('',21), 20)
End
But this SQL statement puts '14 Draft' right after '26'.
Could someone help? Thanks
TRY THIS
declare @t table (Number nvarchar(20)) insert into @t select 'L010' union all select 'L011' union all select 'L011' union all select 'L001' union all select 'L012' union all select '18'
union all select '8' union all select '17'
If all numbers within the string are reasonably small, say not exceeding 10 digits, you may expand all the numbers in the string to be exactly 10 digits:
123A -> 0000000123A
A3B89 -> A0000000003B0000000089
and so on and then sort them
I had something similar, but with the possibility of dashes as leading characters as well as trailing spaces. This code worked for me.
Your WHERE statement is... oddly complex.
It looks like you want to sort by any leading numeric digits in integer order, and then sort by the remainder. If so, you should do that as separate clauses, rather than trying to do it all in one. The specific issue you're having is that you're only allowing for a single-digit number, instead of two or more. (And there's No such thing as two.)
Here's your fix, along with a SQLFiddle, using two separate calculated columns tests for your ORDER BY. (Note that this assumes the numeric portion of
asdNumNew
will fit in aT-SQL int
. If not, you'll need to adjust the CAST and the maximum value on the first ELSE.)