2016-06-09 105 views
2

您好我有有趣的问题,我有一个表中的约1500条记录。列我需要排序对格式SQL排序数字和字符串

字符串Number.number(可选号码)(可选的字符串)

在现实中,这可能是这样的:

AB 2.10.19 
AB 2.10.2 
AB 2.10.20 (I) 
ACA 1.1 
ACA 1.9 (a) V 

我需要一种方法梳理这些使代替

AB 2.10.19 
AB 2.10.2 
AB 2.10.20 (I) 

我得到这个

AB 2.10.2 
AB 2.10.19 
AB 2.10.20 (I) 

由于缺乏标准格式的我不知所措,我怎么可以通过SQL排序如此。

我在只是手动识别新的int列表示排序值的点,除非任何人有任何建议?

我使用SQL Server 2008 R2的

+0

如果你的字符串总是用句点分隔的3个数字,你可以用句点作为分隔符,转换为整数和排序根据结果对其进行分析。 –

+0

不幸的是,它可以AB 1.1或AB 1.1。1或AB 1个 – Andyww

回答

0

试试这个:

SELECT column 
FROM  table 
ORDER BY CASE WHEN SUBSTRING(column,LEN(column)-1,1) = '.' 
       THEN 0 
       ELSE 1 
       END, column 

这将使那些在第二个是.到最后的位置首先在订购任何字符串。

编辑:

关于第二个想法,这不会与领先的 'AB' 工作 'ACA' 等试试这个:

SELECT column 
FROM  table 
ORDER BY SUBSTRING(column,1,2), --This will deal with leading letters up to 2 chars 
     CASE WHEN SUBSTRING(column,LEN(column)-1,1) = '.' 
       THEN 0 
       ELSE 1 
       END, 
     Column 

EDIT2:

要补偿第二个数字集,请使用:

SELECT column 
FROM  table 
ORDER BY substring(column,1,2), 
CASE WHEN substring(column,charindex('.',column) + 2,1) = '.' and substring(column,len(column)-1,1) = '.' THEN 0 
    WHEN substring(column,charindex('.',column) + 2,1) = '.' and substring(column,len(column)-1,1) <> '.' THEN 1 
    WHEN substring(column,charindex('.',column) + 2,1) <> '.' and substring(column,len(column)-1,1) = '.' THEN 2 
ELSE 3 END, column 

基本上,这是通过考虑每个条件来强制进行等级排序的手动方法。

+0

谢谢,几乎有,但其移动的排序问题的第二数量,即 AB 2.1.4 AB 2.1.5 AB 2.10.1 AB 2.10.2 AB 2.2.1 – Andyww

+0

嗯..那一个会更具挑战性。 –

+0

这是否也发生在第一组?即。 AB 2.1.1,AB 20.1.1 –

0

你将需要排序的第一文本标记,然后在第二个文本标记(它不是数字,它的一个字符串,其包括一些数字)然后任选地对任何剩余的文本。

排序正确(如版本号我相信)做第二个符号,你可以使用一个HIERARCHYID:

with t(f) as (
    select 'AB 2.10.19' union all 
    select 'AB 2.10.2' union all 
    select 'AB 2.10.20 (I)' union all 
    select 'AB 2.10.20 (a) Z' union all 
    select 'AB 2.10.21 (a)' union all 
    select 'ACA 1.1' union all 
    select 'ACA 1.9 (a) V' union all 
    select 'AB 4.1' 
) 

select * from t 
order by 
    left(f, charindex(' ', f) - 1), 
    cast('/' + replace(substring(f, charindex(' ', f) + 1, patindex('%[0-9] %', f + ' ') - charindex(' ', f)) , '.', '/') + '/' as hierarchyid), 
    substring(f, patindex('%[0-9] %', f + ' ') + 1, len(f)) 

f 
---------------- 
AB 2.10.2 
AB 2.10.19 
AB 2.10.20 (a) Z 
AB 2.10.20 (I) 
AB 2.10.21 (a) 
AB 4.1 
ACA 1.1 
ACA 1.9 (a) V 
+0

生成此错误: Microsoft.SqlServer.Types.HierarchyIdException:24001:SqlHierarchyId.Parse失败,因为输入字符串'/ 1/1/4-2 /'不是SqlHierarchyId节点的有效字符串表示形式。 – Andyww

+1

您没有提到连字符... –

0

长度相同

SELECT column 
FROM  table 
ORDER BY left(column + replicate('*', 100500), 100500) 
0
添加文本
--get the start and end position of numeric in the string 
with numformat as 
(select val,patindex('%[0-9]%',val) strtnum,len(val)-patindex('%[0-9]%',reverse(val))+1 endnum 
from t 
where patindex('%[0-9]%',val) > 0) --where condition added to exclude records with no numeric part in them 
--get the substring based on the previously calculated start and end positions 
,substrng_to_sort_on as 
(select val, substring(val,strtnum,endnum-strtnum+1) as sub from numformat) 
--Final query to sort based on the 1st,2nd and the optional 3rd numbers in the string 
select val 
from substrng_to_sort_on 
order by 
cast(substring(sub,1,charindex('.',sub)-1) as numeric), --1st number in the string 
cast(substring(sub,charindex('.',sub)+1,charindex('.',reverse(sub))) as numeric), --second number in the string 
cast(reverse(substring(reverse(sub),1,charindex('.',reverse(sub))-1)) as numeric) --third number in the string 

Sample demo

+0

这给了我一个: 传递给LEFT或SUBSTRING函数的长度参数无效。 – Andyww

+0

这是因为列值中没有任何数字部分。根据你的问题,我认为它将始终有数字部分。如果您确认没有数字部分的值可以从结果中排除,则可以编辑该代码。 –

+0

请参阅排除没有数字部分的行的编辑版本。 –