1
1one
2
2two
11
11eleven
11
22twentytwo
111oneeleven
552
3311
A1
A10
A11
A100
AB1
AB10
AB10A
AB100
AB100
B1
B2
B3
B3
B20
B20BB
B21BC
B21
B32
我想按照以下排列顺序在上面的列表中。使用字母数字条目排序VARCHAR列(以数字开头)
1
2
11
11
552
3311
1one
2two
11eleven
22twentytwo
111oneeleven
A1
A10
A11
A100
AB1
AB10
AB10A
AB100
AB100
B1
B2
B3
B3
B20
B20BB
B21BC
B21
B32
查询是:
SELECT
Section,
LEFT(Section, PATINDEX('%[0-9]%', Section) - 1) AS left1,
PATINDEX('%[0-9]%', Section) startindex,
LEN(Section) AS length,
PATINDEX('%[0-9]%', REVERSE(Section)) AS revindex,
LEN(Section) - PATINDEX('%[0-9]%', REVERSE(Section)) + 1 positionofendint,
SUBSTRING(Section, PATINDEX('%[0-9]%', Section), LEN(Section) - PATINDEX('%[0-9]%', REVERSE(Section)) + 1) integerpart,
LEN(Section) - PATINDEX('%[0-9]%', REVERSE(Section)) + 1 - PATINDEX('%[0-9]%', Section) + 1 subintlength,
ISNUMERIC(section) isnumeric1,
LEFT(Section, PATINDEX('[0-9]', Section)) onlyint
FROM
dbo.Section
WHERE
section NOT IN ('33A100', '55B32', '55B1', '55AB100', '99AB10A', '99B21BC', '1B20BB', '6B2B', '3AB1', '4a1', '7A11', '99B3')
--where section not like '%B%' and section not like '%A%' and section not like '%o%' and section not like '%e%'
--ORDER BY section
--, CONVERT(INT,(case when isnumeric(section)=1 then section end))
ORDER BY
LEFT(Section, PATINDEX('%[0-9]%', Section) - 1), -- alphabetical sort
CONVERT(INT, SUBSTRING(Section, PATINDEX('%[0-9]%', Section),
LEN(Section) - PATINDEX('%[0-9]%', REVERSE(Section)) + 1 - PATINDEX('%[0-9]%', Section) + 1)) -- numerical sort
这里,话语开始与字母表排序和旁边号码也是排序,但字开始的数字进行排序,请帮助我如何排序这个词开始与字符的数字。
你不能拥有MySQL和SQL-SERVER,请删除您不使用 – Eli
顺序的关系数据库管理系统的标签你说明你想要的是非常困难的,因为你在排序中混合了数据类型。您只想首先对所有数字值进行排序,并且需要按照数字排序,而不是字符数据,然后您希望包含字符数据的值以相同的方式排序。你真的不能这样做。 –
@SeanLange我相信我已经想出了一个解决方案,但你非常欢迎告诉我,我犯了什么可怕的错误:) **编辑:**自己实现了。我很接近,但是我的字母数字排序会混淆像'AB10A'之前的* AB100'之类的值。 – Santi