-------------------------
--建立時間:08-3-11
--建立人:WJH
--功能:拆分testContent欄位並按照數字排序
-------------------------
--建立暫存資料表
CREATE TABLE #a (testID INT identity(1,1),testContent VARCHAR(50))
--插資料
INSERT #a SELECT 'A10'
INSERT #a SELECT 'A1'
INSERT #a SELECT 'A12'
INSERT #a SELECT 'A2'
INSERT #a SELECT 'A25'
--查詢
SELECT * FROM #a order by Cast((substring(testContent,Cast((patindex('%[0-9]%',testContent)) as int),100)) as int)
--刪除表
DROP TABLE #a
testID textContext
----------- --------------------------------------------------
2 A1
4 A2
1 A10
3 A12
5 A25
以上是利用patindex數字出現的位置,然後截取此位置到最後的(100位很長了)數字,並按照其排序
更複雜些的按照左右兩部分截取的排序
-------------------------
--建立時間:08-3-11
--建立人:WJH
--功能:拆分testContent欄位並按照數字排序
-------------------------
--建立暫存資料表
CREATE TABLE #a (testID INT identity(1,1),testContent VARCHAR(50))
--插資料
INSERT #a SELECT 'A10'
INSERT #a SELECT 'A1'
INSERT #a SELECT 'A12'
INSERT #a SELECT 'A2'
INSERT #a SELECT 'A25'
INSERT #a SELECT 'B10'
INSERT #a SELECT 'B1'
INSERT #a SELECT 'B12'
INSERT #a SELECT 'B2'
INSERT #a SELECT 'B25'
--查詢
select * from #a
order by left(testContent,patindex('%[0-9]%',testContent)-1),
Cast((substring(testContent,Cast((patindex('%[0-9]%',testContent)) as int),100)) as int)
--刪除表
DROP TABLE #a
testID testContent
----------- --------------------------------------------------
2 A1
4 A2
1 A10
3 A12
5 A25
7 B1
9 B2
6 B10
8 B12
10 B25
(所影響的行數為 10 行)