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

后端 未结 11 2008
慢半拍i
慢半拍i 2020-11-27 14:08

I have a VARCHAR column in a SQL Server 2000 database that can contain either letters or numbers. It depends on how the application is configured o

11条回答
  •  不知归路
    2020-11-27 14:35

    you can always convert your varchar-column to bigint as integer might be too short...

    select cast([yourvarchar] as BIGINT)
    

    but you should always care for alpha characters

    where ISNUMERIC([yourvarchar] +'e0') = 1
    

    the +'e0' comes from http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/isnumeric-isint-isnumber

    this would lead to your statement

    SELECT
      *
    FROM
      Table
    ORDER BY
       ISNUMERIC([yourvarchar] +'e0') DESC
     , LEN([yourvarchar]) ASC
    

    the first sorting column will put numeric on top. the second sorts by length, so 10 will preceed 0001 (which is stupid?!)

    this leads to the second version:

    SELECT
          *
        FROM
          Table
        ORDER BY
           ISNUMERIC([yourvarchar] +'e0') DESC
         , RIGHT('00000000000000000000'+[yourvarchar], 20) ASC
    

    the second column now gets right padded with '0', so natural sorting puts integers with leading zeros (0,01,10,0100...) in correct order (correct!) - but all alphas would be enhanced with '0'-chars (performance)

    so third version:

     SELECT
              *
            FROM
              Table
            ORDER BY
               ISNUMERIC([yourvarchar] +'e0') DESC
             , CASE WHEN ISNUMERIC([yourvarchar] +'e0') = 1
                    THEN RIGHT('00000000000000000000' + [yourvarchar], 20) ASC
                    ELSE LTRIM(RTRIM([yourvarchar]))
               END ASC
    

    now numbers first get padded with '0'-chars (of course, the length 20 could be enhanced) - which sorts numbers right - and alphas only get trimmed

提交回复
热议问题