2016-06-09 101 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

請參閱排除沒有數字部分的行的編輯版本。 –