简体   繁体   English

如何进行字母数字排序

[英]How to do alpha numeric sort

Following are my data 以下是我的数据

Transmitter #1
Transmitter #10
Transmitter #11
Transmitter #2
Transmitter #3
Transmitter #4
Transmitter #5
Transmitter #6
Transmitter #7
Transmitter #8
Transmitter #9
Room 1 Transmitter
Allergy Guard – 1
Allergy Guard – 2
Allergy Guard – 3
Deli Counter

How to sort these data as alphanumeric. 如何将这些数据排序为字母数字。

SELECT * FROM #table 
ORDER BY 
Name,
CASE 
    WHEN 
    patindex('%[0-9]%',substring(Name,len(Name),LEN(Name))) =1 
  THEN
        cast(substring(Name,patindex('%[0-9]%', Name),len(Name)) as int)
  END

The following keys should sort these in the order you want them: 以下键应按您希望的顺序对它们进行排序:

  • Sort by everything before the first number (if any) 按第一个数字之前的所有内容排序(如果有)
  • Then by the length ascending 然后按长度递增
  • Then the value ascending 然后价值上升

So: 所以:

order by left(name, patindex('%[0-9]%', name + '0') - 1),
         len(name),
         name

Here is a db<>fiddle. 是一个db <>小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM