2017-12-18 154 views
0

我有像(1100,2014,4000)的字符串字段我想分離逗號和總結每個數字彼此例如: 1100,2014,4000如何在SQL Server中使用逗號和數字?

1 1 0 0 
2 0 1 4 
4 0 0 0 

結果

@first =1+2+4=7 
@second= 1+0+0=1 
@third=0+1+0=1 
@forth=0+4+0=4 
+0

寫一個函數,其可以返回每個數字的總和。 –

+0

爲什麼要存儲這樣的數據? – Mureinik

+0

你的桌子是怎樣的? '1100,2014,4000'是一個單獨的字符串或3個單獨的行 – Squirrel

回答

2

試試這個,

DECLARE @Table TABLE(Value VARCHAR(20)) 
INSERT INTO @Table VALUES('1100,2014,4000') 

DECLARE @First INT, @Second INT, @Third INT, @Fourth INT 

SELECT Split.a.value('.', 'VARCHAR(100)') AS Data 
INTO #temp 
FROM 
(
    SELECT CAST ('<M>' + REPLACE(Value, ',', '</M><M>') + '</M>' AS XML) AS Value 
    FROM @Table 
) AS A CROSS APPLY Value.nodes ('/M') AS Split(a); 


SELECT @First=SUM(Data/1000) 
    ,@Second=SUM((Data%1000)/100) 
    ,@Third=SUM((Data%100)/10) 
    ,@Fourth=SUM((Data%10)) 
FROM #temp 

SELECT @First, @Second, @Third, @Fourth 

DROP TABLE #temp 
+0

感謝您的回答,它的工作原理;) –

2

這可能有助於獲得值之和是這樣的:

SELECT SUM(CAST(SUBSTRING(X.A, 1, 1) AS INT)) first, 
    SUM(CAST(SUBSTRING(X.A, 2, 1) AS INT)) second, 
    SUM(CAST(SUBSTRING(X.A, 3, 1) AS INT)) third, 
    SUM(CAST(SUBSTRING(X.A, 4, 1) AS INT)) fourth 
FROM (SELECT '1100' A 
     UNION 
     SELECT '2014' A 
     UNION 
     SELECT '4000' A 
    ) X 
1

爲此,首先,採用傑夫MODEN的DelimitedSplit8K(因爲我不是k現在是什麼版本的SQl服務器)。其次,你的邏輯看起來有點過分。你說變量@third的值是0+4+0,但是,這是整數的第4個字符。另外,這個假設所有整數是4個字符長。

WITH VTE AS(
    SELECT * 
    FROM (VALUES('1100,2014,4000')) v(DSn)) --This is your samnple data 
SELECT SUM(CONVERT(int,SUBSTRING(RIGHT('0000' + DS.Item,4),N.I, 1))) AS [Sum] 
FROM VTE 
    CROSS APPLY dbo.DelimitedSplit8K (VTE.DSn,',') DS 
    CROSS APPLY (VALUES (1),(2),(3),(4)) N(I) 
GROUP BY N.I; 
+0

我錯過了@third = 1,@ forth = 4 感謝您的回答 和是的所有他們有4個字符 –

1

您可以使用此函數將所有項目排在表格中。

您可以創建此功能,並嘗試再 SELECT * FROM dbo.string2table( '1243,1234,2343', '')

CREATE FUNCTION [dbo].[string2table] 
(
    @string VARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(
    data VARCHAR(256) 
) 
BEGIN 

    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 

    WHILE @start < LEN(@string) + 1 BEGIN 
     IF @end = 0 
      SET @end = LEN(@string) + 1 

     INSERT INTO @output (data) 
     VALUES(SUBSTRING(@string, @start, @end - @start)) 
     SET @start = @end + 1 
     SET @end = CHARINDEX(@delimiter, @string, @start) 
    END 

    RETURN 

END 
+0

我建議不要使用'CURSOR'。它們天生就很慢。 – Larnu

+0

好的Larnu,我同意你的看法,特別是我不使用遊標等待死鎖,但是這個函數已經寫好了,我要求解決這個問題。謝謝。 –

2

繼具有多個SQL CTE表達式的Select語句可以在numbers table functionSQL split string function的幫助下用於使用「,」分隔數字列表,作爲分隔符

對於進一步分割各數到它的標號,我更願意使用子串函數

with cte as (
    select 
    * 
    from NumbersList n, 
    dbo.NumbersTable(1,4,1) as nt 
), splitted as (
select 
    list, 
    id, 
    i, 
    substring(val,i,1) val 
from cte 
cross apply dbo.Split(list,',') v 
) 
select 
    distinct 
    list, 
    i, 
    sum(cast(val as int)) over (partition by list, i) sumOf 
from splitted 

輸出爲以下項

insert into NumbersList select '1100,2014,4000' 
insert into NumbersList select '1111,2222,3456' 

是如下

enter image description here

相關問題