以varchar字母顺序对数据进行排序

时间:2015-09-01 09:31:39

标签: sql sql-server-2008 sql-server-2008-r2

我有以下数据表。

 1
 AAAAA01 
 AAAAA01
 AAAAA01
 B21
 AAAAAA1
 B3
 AB100

我想按以下顺序对数据进行排序

 AAAAAA1
 AAAAA01
 AAAAA01
 AAAAA01
 AB100
 B21
 B3
 1

我写了一个声明,但没有给我正确的结果。

    Select
    *
from
    dbo.Section
order by      
    CASE    
      WHEN not Section like '%[^0-9]%' THEN CONVERT(int,Section)
      WHEN  Section like '[0-9]%' THEN CONVERT(int,SUBSTRING(Section,1,PATINDEX('%[A-Z]%',Section)-900000))
    END

为了你的帮助,我提供表格的脚本

  INSERT [dbo].[Section] ([Section]) VALUES (N'1')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAA01')
INSERT [dbo].[Section] ([Section]) VALUES (N'B21')
INSERT [dbo].[Section] ([Section]) VALUES (N'AAAAAA1')
INSERT [dbo].[Section] ([Section]) VALUES (N'B3')
INSERT [dbo].[Section] ([Section]) VALUES (N'AB100')
INSERT [dbo].[Section] ([Section]) VALUES (N'2')
INSERT [dbo].[Section] ([Section]) VALUES (N'B1')
INSERT [dbo].[Section] ([Section]) VALUES (N'B32')
INSERT [dbo].[Section] ([Section]) VALUES (N'11')
INSERT [dbo].[Section] ([Section]) VALUES (N'A10')
INSERT [dbo].[Section] ([Section]) VALUES (N'ABAAAA') 

你能告诉我这怎么能给我正确的答案。

5 个答案:

答案 0 :(得分:1)

请尝试:

Select    
    *
from
    dbo.Section
order by
    REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE
    (REPLACE ([Section], '0', 'ZZ0'),
    '1', 'ZZ1'),
    '2', 'ZZ2'),
    '3', 'ZZ3'),
    '4', 'ZZ4'),
    '5', 'ZZ5'),
    '6', 'ZZ6'),
    '7', 'ZZ7'),
    '8', 'ZZ8'),
    '9', 'ZZ9'),
    [Section]

Sqlfiddle

选择新测试数据的结果: enter image description here

请检查是否正确。

答案 1 :(得分:1)

这是另一种方式。假设您的数字字符串大于-100000000。

SELECT YourString
FROM YourTable
ORDER BY CASE WHEN YourString LIKE '[0-9]%' THEN 
                        CONVERT(int, YourString) ELSE -100000000  END, YourString

答案 2 :(得分:0)

我试图将你的字段分成两个,alpha和num。为了按Alpha排序查询,首先是Num,第二是 但AAAAA1以我的逻辑AAAA01之后......你是否有一个明确的规则来解释为什么在AAAA01之前你想要AAAAA1

SELECT SECTION.section,
        CASE
            WHEN section NOT LIKE '%[^A-Z]'
            THEN section
            WHEN section LIKE '[A-Z]%'
            THEN
               LEFT (section, PATINDEX ('%[^A-Z]%', section)-1)                  
            ELSE
            'ZZZZZZZZZZZZZ'
         END
            AS Alpha,
            CASE
            WHEN ISNUMERIC(section)=1 
            THEN CAST( section AS int)
            WHEN SECTION.section LIKE '%[0-9]'
            THEN
               CAST( RIGHT (section, PATINDEX ('%[^0-9]%', REVERSE (section))-1 ) AS INT)
            ELSE
               0       
         END
            AS Num

    FROM SECTION
ORDER BY Alpha, Num;

Please see the SQL Fiddle

答案 3 :(得分:0)

这个SQL怎么样?我尝试了你的数据并且它返回了正确的订单

SELECT section
FROM section
ORDER BY LEN(LEFT(Section, PATINDEX('%[0-9]%', Section)-1)) DESC

回复@ A.Goutam的评论如下(对不起,我不知道如何在评论中添加图片)

我刚刚复制了@ A.Goutam提供的数据并直接在我的sql server中试用了它,如下所示

enter image description here

使用新数据的新SQL查询

SELECT section AS section
FROM section
ORDER BY LEN(section + '1') DESC
    , LEN(LEFT(section + '1', PATINDEX('%[0-9]%', section + '1')-1)) DESC
    , CONVERT(INT,SUBSTRING(section + '1',PATINDEX('%[0-9]%',section + '1'),LEN(section + '1'))) DESC

结果:

AAAAAA1
AAAAA01
AAAAA01
AAAAA01
AAAAA01
AAAAA01
ABAAAA
AB100
B32
B21
A10
B3
B1
11
2
1

答案 4 :(得分:0)

您也可以通过临时表来完成您尝试获得的内容。但是,对于较大的记录,这可能会增加性能开销。所以,我建议你谨慎使用它。

CREATE table #temp
(
    Section varchar(20)
)
INSERT INTO #temp
   SELECT * FROM dbo.Section WHERE section LIKE '[A-Z]%' order by section ASC
INSERT INTO #temp
   SELECT * FROM dbo.Section WHERE section LIKE '[0-9]%' order by section ASC

SELECT * FROM #temp
drop table #temp