SQL for ordering by number - 1,2,3,4 etc instead of 1,10,11,12

前端 未结 8 1869
清歌不尽
清歌不尽 2020-11-30 19:13

I’m attempting to order by a number column in my database which has values 1-999

When I use

ORDER_BY registration_no ASC

I get….

相关标签:
8条回答
  • 2020-11-30 19:49

    This problem is just because you have declared the column in CHAR, VARCHAR or TEXT datatype. Just change the datatype to INT, BIGINT etc. This is will solved the problem of your custom ordering.

    0 讨论(0)
  • 2020-11-30 19:55

    Sometimes you just don't have a choice about having to store numbers mixed with text. In one of our applications, the web site host we use for our e-commerce site makes filters dynamically out of lists. There is no option to sort by any field but the displayed text. When we wanted filters built off a list that said things like 2" to 8" 9" to 12" 13" to 15" etc, we needed it to sort 2-9-13, not 13-2-9 as it will when reading the numeric values. So I used the SQL Server Replicate function along with the length of the longest number to pad any shorter numbers with a leading space. Now 20 is sorted after 3, and so on.

    I was working with a view that gave me the minimum and maximum lengths, widths, etc for the item type and class, and here is an example of how I did the text. (LBnLow and LBnHigh are the Low and High end of the 5 length brackets.)

    REPLICATE(' ', LEN(LB5Low) - LEN(LB1High)) + CONVERT(NVARCHAR(4), LB1High) + '" and Under' AS L1Text,
    REPLICATE(' ', LEN(LB5Low) - LEN(LB2Low)) + CONVERT(NVARCHAR(4), LB2Low) + '" to ' + CONVERT(NVARCHAR(4), LB2High) + '"' AS L2Text,
    REPLICATE(' ', LEN(LB5Low) - LEN(LB3Low)) + CONVERT(NVARCHAR(4), LB3Low) + '" to ' + CONVERT(NVARCHAR(4), LB3High) + '"' AS L3Text,
    REPLICATE(' ', LEN(LB5Low) - LEN(LB4Low)) + CONVERT(NVARCHAR(4), LB4Low) + '" to ' + CONVERT(NVARCHAR(4), LB4High) + '"' AS L4Text,
    CONVERT(NVARCHAR(4), LB5Low) + '" and Over' AS L5Text
    
    0 讨论(0)
  • 2020-11-30 19:56

    If you are using SQL Server:

    ORDER_BY cast(registration_no as int) ASC
    
    0 讨论(0)
  • 2020-11-30 19:58
    ORDER_BY cast(registration_no as unsigned) ASC
    

    gives the desired result with warnings.

    Hence, better to go for

    ORDER_BY registration_no + 0 ASC
    

    for a clean result without any SQL warnings.

    0 讨论(0)
  • 2020-11-30 20:01

    One way to order by positive integers, when they are stored as varchar, is to order by the length first and then the value:

    order by len(registration_no), registration_no
    

    This is particularly useful when the column might contain non-numeric values.

    Note: in some databases, the function to get the length of a string might be called length() instead of len().

    0 讨论(0)
  • 2020-11-30 20:01
    ORDER_BY cast(registration_no as unsigned) ASC
    

    explicitly converts the value to a number. Another possibility to achieve the same would be

    ORDER_BY registration_no + 0 ASC
    

    which will force an implicit conversation.

    Actually you should check the table definition and change it. You can change the data type to int like this

    ALTER TABLE your_table MODIFY COLUMN registration_no int;
    
    0 讨论(0)
提交回复
热议问题