How To Sum Numbers With Commas In SQL Server?

时间:2017-12-18 08:25:42

标签: sql sql-server

i have string field like (1100,2014,4000) i want to separate comma and sum every number with each other for instance: 1100,2014,4000

public String testString(@RequestBody String jsonString) {
    System.out.println(jsonString);
    return jsonString;
}

result

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

5 个答案:

答案 0 :(得分:3)

这可能有助于获得如下值的总和:

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 :(得分: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

答案 2 :(得分:2)

在具有多个SQL CTE表达式的Select语句之后,可以在numbers table functionSQL split string function的帮助下使用&#34;,&#34;来分割数字列表。作为分隔符

为了进一步将每个数字拆分成数字,我更喜欢使用子字符串函数

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

答案 3 :(得分:1)

这首先使用了Jeff Moden的DelimitedSplit8K(因为我不知道您使用的是什么版本的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;

答案 4 :(得分:1)

您可以将此功能用于表格中的所有项目。

您可以创建此功能然后尝试 select * from dbo.string2table(&#39; 1243,1234,2343&#39;,&#39;,&#39;)

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