我在使用SQL中的字符串和数值对varchar
进行排序时遇到问题。
示例:
SELECT Section
FROM
(
SELECT 'SECTION 1--INTRODUCTION' Section UNION ALL
SELECT 'SECTION 10--ALARMS' Section UNION ALL
SELECT 'SECTION 11--SYNCHRONIZATION' Section UNION ALL
SELECT 'SECTION 12--POWER SYSTEMS' Section UNION ALL
SELECT 'SECTION 13--GROUNDING AND BONDING' Section UNION ALL
SELECT 'SECTION 2—TELEPHONE EQUIPMENT ORDERS (TEOs)' Section UNION ALL
SELECT 'SECTION 3--DETAIL ENGINEERING SPECIFICATION REQUIREMENTS' Section UNION ALL
SELECT 'SECTION 4--CENTRAL OFFICE EQUIPMENT RECORDS' Section UNION ALL
SELECT 'SECTION 5--EQUIPMENT LAYOUT' Section UNION ALL
SELECT 'SECTION 6--CARRIER COMMUNICATIONS SPACE EQUIPMENT BUILDING' Section UNION ALL
SELECT 'SECTION 7--WIRE AND CABLE REQUIREMENTS' Section UNION ALL
SELECT 'SECTION 8--CABLE RACK, AUXILIARY FRAMING AND LIGHTING SYSTEMS' Section UNION ALL
SELECT 'SECTION 9 - CROSS CONNECT SYSTEMS' Section UNION ALL
SELECT '15 MOPS' Section UNION ALL
SELECT '16 CLECS' Section UNION ALL
SELECT '2 GENERAL REQUIREMENTS' Section
) AS TBL
ORDER BY
Section
以上查询输出15M,16 C,2G,1,10,11,12,13,2 ...但我需要我的最终输出如下所示
2 GENERAL REQUIREMENTS
15 MOPS
16 CLECS
SECTION 1--INTRODUCTION
SECTION 2—TELEPHONE EQUIPMENT ORDERS (TEOs)
SECTION 3--DETAIL ENGINEERING SPECIFICATION REQUIREMENTS
SECTION 4--CENTRAL OFFICE EQUIPMENT RECORDS
SECTION 5--EQUIPMENT LAYOUT
SECTION 6--CARRIER COMMUNICATIONS SPACE EQUIPMENT BUILDING
SECTION 7--WIRE AND CABLE REQUIREMENTS
SECTION 8--CABLE RACK, AUXILIARY FRAMING AND LIGHTING SYSTEMS
SECTION 9 - CROSS CONNECT SYSTEMS
SECTION 10--ALARMS
SECTION 11--SYNCHRONIZATION
SECTION 12--POWER SYSTEMS
SECTION 13--GROUNDING AND BONDING
请注意,用户提出的排列和字符不一致。
我已经尝试过这篇文章中的一个答案,但没有任何作用:
SQL Server 2008 - order by strings with number numerically
感谢任何帮助。
谢谢!
答案 0 :(得分:1)
根据新要求编辑。
;WITH cte AS (
SELECT 'SECTION 1--INTRODUCTION' Section UNION ALL
SELECT 'SECTION 10--ALARMS' Section UNION ALL
SELECT 'SECTION 11--SYNCHRONIZATION' Section UNION ALL
SELECT 'SECTION 12--POWER SYSTEMS' Section UNION ALL
SELECT 'SECTION 13--GROUNDING AND BONDING' Section UNION ALL
SELECT 'SECTION 2—TELEPHONE EQUIPMENT ORDERS (TEOs)' Section UNION ALL
SELECT 'SECTION 3--DETAIL ENGINEERING SPECIFICATION REQUIREMENTS' Section UNION ALL
SELECT 'SECTION 4--CENTRAL OFFICE EQUIPMENT RECORDS' Section UNION ALL
SELECT 'SECTION 5--EQUIPMENT LAYOUT' Section UNION ALL
SELECT 'SECTION 6--CARRIER COMMUNICATIONS SPACE EQUIPMENT BUILDING' Section UNION ALL
SELECT 'SECTION 7--WIRE AND CABLE REQUIREMENTS' Section UNION ALL
SELECT 'SECTION 8--CABLE RACK, AUXILIARY FRAMING AND LIGHTING SYSTEMS' Section UNION ALL
SELECT 'SECTION 9 - CROSS CONNECT SYSTEMS' Section UNION ALL
SELECT '15 MOPS' Section UNION ALL
SELECT '16 CLECS' Section UNION ALL
SELECT '2 GENERAL REQUIREMENTS' Section
)
SELECT *
FROM
cte
ORDER BY
CAST (
LEFT(
RIGHT(SECTION,LEN(Section) - PATINDEX('%[0-9]%',Section) + 1)
,PATINDEX('%[^0-9]%'
,RIGHT(SECTION,LEN(Section) - PATINDEX('%[0-9]%',Section) + 1) ) - 1
)
AS INT)
这个想法使用PATINDEX
来查找第一次出现的数字,然后再次使用PATINDEX
来查找第一次出现的非数字字符。