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
、Wordがアルファベットで開始番号もソートされているが、言葉は数字で開始がソートされていないことを、ソートや隣にいる、ソートする方法で私を助けてください単語は文字で数字を開始しました。
あなたはMYSQLとSQL-SERVERの両方を持つことはできません、あなたは – Eli
順序を使用していないRDMSのタグを削除してくださいあなたがあなたの並べ替えでデータ型を混合しているので、あなたが望むと言っているのは非常に困難です。すべての数値のみを最初にソートしたいのであって、文字データではなく数字のようにソートする必要がある場合、文字データを含む値で同じ方法でソートする必要があります。あなたは本当にこれをすることはできません。 –
@SeanLange私は解決策を見つけたと信じていますが、私はひどく間違っていたところを教えてくれます。**編集:**自分で実現しました。私は近づいていますが、英数字のソートはAB10Aのような値にミックスアップされます。 – Santi