0

I need to make the order by for the nvarchar column. The issue is this that the column doesn't have the same pattern of entry, example of some rows of the column are

12    
9     
TEF      
78F    
T2R

How to sort this column by order By clause? The only relief I have is that this column contains only numeric and alpha numeric characters.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
  • 2
    What is the issue you are facing? Is `order by ` not working? – Sachin Shanbhag Dec 09 '10 at 11:19
  • @Sachin No it is not working correctly. By doing that it feels like that it is not at all sorting the records – Zerotoinfinity Dec 09 '10 at 11:28
  • So how does it sort your example column? – James Wiseman Dec 09 '10 at 11:29
  • 1
    What is the desired output then for this input? And why? – gbn Dec 09 '10 at 11:29
  • @Zerotoinfinite - Can you show some sample results you are getting and the result which you are expecting? That would help. – Sachin Shanbhag Dec 09 '10 at 11:29
  • 1
    I was adviced by my client to sort it. He is expecting output like first numeric value than alphanumeric. In this case. 9 , 12,78F, T2R, TEF – Zerotoinfinity Dec 09 '10 at 11:34
  • There are many different possible output orders that *might* be desired, other than the string sorting (what you'd get by just saying `order by column`). For instance, you might want all purely numeric values to sort first, and sorted numerically (so 2 comes between 1 and 10), or more complex rules (e.g. all values *starting* numerically sorted by numeric rules, then by string rules for the remained). You need to tell us what rules you think "make sense" in your case. – Damien_The_Unbeliever Dec 09 '10 at 11:36

3 Answers3

5
ORDER BY
   RIGHT(REPLICATE(N' ', 500) + MyValue, 500)

So

9
12
78F
T2R
TEF 

becomes

            9
           12
          78F
          T2R
          TEF 

Which will sort correctly

You can't rely on ISNUMERIC

gbn
  • 422,506
  • 82
  • 585
  • 676
2
select
    *
from
    table
order by
    CASE
       WHEN not column like '%[^0-9]%' THEN CONVERT(int,column)
       WHEN column like '[0-9]%' THEN CONVERT(int,SUBSTRING(column,1,PATINDEX('%[A-Z]%',column)-1))
    END,
    CASE
       WHEN not column like '%[^0-9]%' THEN NULL
       WHEN column like '[0-9]%' THEN SUBSTRING(column,PATINDEX('%[A-Z]%',column),9000)
       ELSE column
    END
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • +1 for most sensible answer but even then, what do you do with decimals, scientific notation etc? The client would do well to define the desired behaviour – Tao Jun 07 '11 at 13:29
0

I'm guessing that you issue is that its not sorting the numbers and text correctly.

This article explains one approach:

How do I sort a VARCHAR column in SQL server that contains numbers?

From the article:

select MyColumn
from MyTable
order by 
    case IsNumeric(MyColumn) 
        when 1 then Replicate(Char(0), 100 - Len(MyColumn)) + MyColumn
        else MyColumn
    end
Community
  • 1
  • 1
James Wiseman
  • 29,946
  • 17
  • 95
  • 158