SQL Server - sum comma separated value from a column - sql

There is a column in database which contains comma separated values like: 0.00,12.45,14.33 and so on.
I need to sum this inside a stored procedure. One way which I can think of is to split and convert it into a table using a function and then sum it.
Any other ideas?

Using Sql Server 2005+ CTE you can create a recursive select, something like
DECLARE #Table TABLE(
ID INT,
Vals VARCHAR(100)
)
INSERT INTO #Table SELECT 1, '0.00,12.45,14.33'
INSERT INTO #Table SELECT 2, '1,2,3,4'
;WITH ValList AS(
SELECT ID,
CAST(LEFT(Vals,PATINDEX('%,%', Vals) - 1) AS FLOAT) Val,
RIGHT(Vals,LEN(Vals) - PATINDEX('%,%', Vals)) Remainder
FROM #Table
UNION ALL
SELECT ID,
CAST(LEFT(Remainder,CASE WHEN PATINDEX('%,%', Remainder) = 0 THEN LEN(Remainder) ELSE PATINDEX('%,%', Remainder) - 1 END) AS FLOAT) Val,
RIGHT(Remainder,CASE WHEN PATINDEX('%,%', Remainder) = 0 THEN 0 ELSE LEN(Remainder) - PATINDEX('%,%', Remainder) END) Remainder
FROM ValList
WHERE LEN(Remainder) > 0
)
SELECT ID,
SUM(Val)
FROM ValList
GROUP BY ID
OUTPUT
ID Total
----------- ----------------------
1 26.78
2 10

within a function you could try something like this, totally unsure if it will work tho!
CREATE FUNCTION ufn_sum_csv(#string varchar(100))
RETURNS #result int
AS BEGIN
EXEC 'SELECT #result = ' + REPLACE(#string,',','+')
RETURN
Can't try it out on this comp.

Related

How To Sum Numbers With Commas In 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
1 1 0 0
2 0 1 4
4 0 0 0
result
#first =1+2+4=7
#second= 1+0+0=1
#third=0+1+0=1
#forth=0+4+0=4
This might help to get sum of values like this :
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
Try this,
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
Following Select statement with multiple SQL CTE expressions can be used with the help of a numbers table function and a SQL split string function for splitting numbers list using "," as seperator
For further splitting each number into its numerals, I preferred to use substring 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
The output for following entries
insert into NumbersList select '1100,2014,4000'
insert into NumbersList select '1111,2222,3456'
is as follows
This, firstly, uses Jeff Moden's DelimitedSplit8K (as I don't know what version of SQl Server you're on). Secondly, your logic seems a little off. You say that the value for your variable #third is 0+4+0, however, that's the 4th characters of the ints. Also, this assumes that all integers are 4 characters long.
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;
You can use this function for all item to row in a table.
you can create this function and try then
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

Add rows to result set from comma delimited data

I have a select statement returning data in the following format- (from a bad database design)
ID Numbers
3 6,7,8
and I need to tweak it to return
ID Number
3 6
3 7
3 8
to fix it. What's the best way to do this? I do not need a permanent function, I just need the result set from a select query to import into a new database.
thanks!
Since you are using SQL Server 2015, your best option is to write a UDF like so:
CREATE FUNCTION [dbo].[udfSplit]
(
#sInputList VARCHAR(8000) -- List of delimited items
,
#sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
)
RETURNS #List TABLE ( item VARCHAR(8000) )
BEGIN
DECLARE #sItem VARCHAR(8000);
WHILE CHARINDEX(#sDelimiter, #sInputList, 0) <> 0
BEGIN
SELECT #sItem = RTRIM(LTRIM(SUBSTRING(#sInputList, 1,
CHARINDEX(#sDelimiter,
#sInputList, 0)
- 1))) ,
#sInputList = RTRIM(LTRIM(SUBSTRING(#sInputList,
CHARINDEX(#sDelimiter,
#sInputList, 0)
+ LEN(#sDelimiter),
LEN(#sInputList))));
IF LEN(#sItem) > 0
INSERT INTO #List
SELECT #sItem;
END;
IF LEN(#sInputList) > 0
INSERT INTO #List
SELECT #sInputList; -- Put the last item in
RETURN;
END;
And use it like so:
SELECT ID ,
item
FROM ( SELECT 3 AS ID ,
'6,7,8' AS Numbers
) x
CROSS APPLY udfSplit(x.Numbers, ',');
Try this:
DECLARE #YourTable Table (
ID INT IDENTITY(1,1)
, Number VARCHAR(10)
)
INSERT INTO #YourTable
VALUES
('1,2,3')
, ('4,5,6,7')
, ('8,9')
, ('10')
;WITH CTE
AS
(
SELECT 1 AS ID
UNION ALL
SELECT ID + 1 FROM CTE
WHERE ID < 100
)
SELECT
x.ID
, SUBSTRING(x.Number, t.ID, CHARINDEX(',', x.Number + ',', t.ID) - t.ID) AS Number
FROM #YourTable x
JOIN CTE t
ON t.ID <= DATALENGTH(x.Number)+1
AND SUBSTRING(',' + x.Number, t.ID, 1) = ','
ORDER BY ID, Number
More information can be found here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Split string and number columns

Let's say I have a table such as
ItemID ClassID
------------------------
1 10, 13, 12
2 5, 7
and would like to copy the data to another table like so
ItemID Numbering ClassID
----------------------------------
1 1 10
1 2 13
1 3 12
2 1 5
2 2 7
Separating the comma-delimited ClassID field into individual rows, retaining the order they had in the first table
Populating the Numbering row on insert. The Numbering column has sequential integers for each batch of ClassID and is why ClassID needs to be kept in order.
I have attempted this with the following function:
CREATE FUNCTION dbo.Split
(
#String NVARCHAR(MAX)
)
RETURNS #SplittedValues TABLE(
Value INT
)
AS
BEGIN
DECLARE #SplitLength INT
DECLARE #Delimiter VARCHAR(10)
SET #Delimiter = ','
WHILE len(#String) > 0
BEGIN
SELECT #SplitLength = (CASE charindex(#Delimiter, #String)
WHEN 0 THEN
datalength(#String) / 2
ELSE
charindex(#Delimiter, #String) - 1
END)
INSERT INTO #SplittedValues
SELECT cast(substring(#String, 1, #SplitLength) AS INTEGER)
WHERE
ltrim(rtrim(isnull(substring(#String, 1, #SplitLength), ''))) <> '';
SELECT #String = (CASE ((datalength(#String) / 2) - #SplitLength)
WHEN 0 THEN
''
ELSE
right(#String, (datalength(#String) / 2) - #SplitLength - 1)
END)
END
RETURN
END
but it only partly works. It copies the rows the correct amount of times (i.e. three times for ItemID=1, and twice for ItemID=2 in the above example), but they are exact copies of the row (all saying '10, 13, 12') and the comma-delimited parts are not split up. There is also nothing in the function to add to the Numbering column.
So, I have two questions: How do I modify the above function to split up the ClassID string, and what do I add to correctly increment the Numbering column?
Thanks!
I'd use a recursive CTE to do it.
WITH SplitCTE AS
(
SELECT
itemid,
LEFT(ClassID,CHARINDEX(',',ClassID)-1) AS ClassID
,RIGHT(ClassID,LEN(ClassID)-CHARINDEX(',',ClassID)) AS remaining
FROM table1
WHERE ClassID IS NOT NULL AND CHARINDEX(',',ClassID)>0
UNION ALL
SELECT
itemid,
LEFT(remaining,CHARINDEX(',',remaining)-1)
,RIGHT(remaining,LEN(remaining)-CHARINDEX(',',remaining))
FROM SplitCTE
WHERE remaining IS NOT NULL AND CHARINDEX(',',remaining)>0
UNION ALL
SELECT
itemid,remaining,null
FROM SplitCTE
WHERE remaining IS NOT NULL AND CHARINDEX(',',remaining)=0
)
SELECT
itemid,
row_number() over (partition by itemid order by cast(classid as int) asc) as Numbering,
cast (ClassID as int) as ClassID
FROM
SplitCTE
UNION ALL
select
ItemId,
1,
cast(classid as int)
FROM table1
WHERE ClassID IS NOT NULL AND CHARINDEX(',',ClassID) = 0
SQL Fiddle
DECLARE #t TABLE( ID INT IDENTITY, data VARCHAR(50))
INSERT INTO #t(data) SELECT '10, 13, 12'
INSERT INTO #t(data) SELECT '5, 7'
select F1.id,O.splitdata, ROW_NUMBER() OVER(PARTITION BY ID ORDER BY (SELECT 1))
from (
select *,cast(''+replace(F.data,',','')+'' as XML) as xmlfilter from #t F
)F1
cross apply
(
select fdata.D.value('.','varchar(50)') as splitdata from f1.xmlfilter.nodes('X') as fdata(D)
) O

Return Distinct Rows That Contain The Same Value/Character In SQL

I have a bit of a tricky situation. I have a column that contains a pipe delimited set of numbers in numerous rows in a table. For example:
Courses
-------------------
1|2
1|2|3
1|2|8
10
11
11|12
What I want to achieve is to return rows where the number only appears once in my output.
Ideally, I want to try and carry this out using SQL rather than having to carry out checks at a web application level. Carrying out a DISTINCT does not achieve what I want.
The desired output would be:
Courses
-------------------
1
2
3
8
10
11
12
I would appreciated if anyone can guide me in the right direction.
Thanks.
Please try:
declare #tbl as table(Courses nvarchar(max))
insert into #tbl values
('1|2'),
('1|2|3'),
('1|2|8'),
('10'),
('11'),
('11|12')
select * from #tbl
SELECT
DISTINCT CAST(Split.a.value('.', 'VARCHAR(100)') AS INT) AS CVS
FROM
(
SELECT CAST ('<M>' + REPLACE(Courses, '|', '</M><M>') + '</M>' AS XML) AS CVS
FROM #tbl
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
ORDER BY 1
Try this one -
SET NOCOUNT ON;
DECLARE #temp TABLE
(
string VARCHAR(500)
)
DECLARE #Separator CHAR(1)
SELECT #Separator = '|'
INSERT INTO #temp (string)
VALUES
('1|2'),
('1|2|3'),
('1|2|8'),
('10'),
('11'),
('11|12')
-- 1. XML
SELECT p.value('(./s)[1]', 'VARCHAR(500)')
FROM (
SELECT field = CAST('<r><s>' + REPLACE(t.string, #Separator, '</s></r><r><s>') + '</s></r>' AS XML)
FROM #temp t
) d
CROSS APPLY field.nodes('/r') t(p)
-- 2. CTE
;WITH a AS
(
SELECT
start_pos = 1
, end_pos = CHARINDEX(#Separator, t.string)
, t.string
FROM #temp t
UNION ALL
SELECT
end_pos + 1
, CHARINDEX(#Separator, string, end_pos + 1)
, string
FROM a
WHERE end_pos > 0
)
SELECT d.name
FROM (
SELECT
name = SUBSTRING(
string
, start_pos
, ABS(end_pos - start_pos)
)
FROM a
) d
WHERE d.name != ''
Try this :
create table course (courses varchar(100))
insert into course values('1|2')
insert into course values('1|2|3')
insert into course values('1|2|8')
insert into course values('10')
insert into course values('11')
insert into course values('11|12')
Declare #col varchar(200)
SELECT
#col=(
SELECT DISTINCT c.courses + '|'
FROM course c
FOR XML PATH('')
);
select * from course
;with demo as(
select cast(substring(#col,1,charindex('|',#col,1)-1) AS INT) cou,charindex('|',#col,1) pos
union all
select cast(substring(#col,pos+1,charindex('|',#col,pos+1)-pos-1)AS INT) cou,charindex('|',#col,pos+1) pos
from demo where pos<LEN(#col))
select distinct cou from demo
Could not manage without recursion :( Something like this could do the trich?
WITH splitNum(num, r)
AS
(
SELECT
SUBSTRING(<field>,1, CHARINDEX('|', <field>)-1) num,
SUBSTRING(<field>,CHARINDEX('|', <field>)+1, len(<field>)) r
FROM <yourtable> as a
UNION ALL
SELECT
SUBSTRING(r,1, CHARINDEX('|', r)-1) num,
SUBSTRING(r,CHARINDEX('|', r)+1, len(r)) r
FROM <yourtable> b
WHERE CHARINDEX('|', r) > 0
inner join splitNum as c on <whatevertheprimarykeyis>
)
SELECT distinct num FROM splitNum
Didn't make it run, but it should do the trick, just replace the and with the correct info
One way would be to use a recursive CTE:
with cte as
(select cast(case charindex('|',courses) when 0 then courses
else left(courses,charindex('|',courses)-1) end as int) course,
case charindex('|',courses) when 0 then ''
else right(courses,len(courses)-charindex('|',courses)) end courses
from courses
union all
select cast(case charindex('|',courses) when 0 then courses
else left(courses,charindex('|',courses)-1) end as int) course,
case charindex('|',courses) when 0 then ''
else right(courses,len(courses)-charindex('|',courses)) end courses
from cte
where len(courses)>0)
select distinct course from cte
SQLFiddle here.

Tricky SQL query requiring search for contains

I have data such as this:
Inventors column in my table
Hundley; Edward; Ana
Isler; Hunsberger
Hunsberger;Hundley
Names are separated by ;. I want to write a SQL query which sums up the count.
Eg. The result should be:
Hundley 2
Isler 1
Hunsberger 2
Edward 1
Ana 1
I could do a group by but this is not a simple group by as you can see. Any ideas/thoughts on how to get this output?
Edit: Changed results so it doesn't create any confusion that a row only contains 2 names.
You can take a look at this. I certainly do not recommend this way if you have lots of data, BUT you can do some modifications and use it and it works like a charm!
This is the new code for supporting unlimited splits:
Declare #Table Table (
Name Nvarchar(50)
);
Insert #Table (
Name
) Select 'Hundley; Edward; Anna'
Union Select 'Isler; Hunsberger'
Union Select 'Hunsberger; Hundley'
Union Select 'Anna'
;
With Result (
Part
, Remained
, [Index]
, Level
) As (
Select Case When CharIndex(';', Name, 1) = 0
Then Name
Else Left(Name, CharIndex(';', Name, 1) - 1)
End
, Right(Name, Len(Name) - CharIndex(';', Name, 1))
, CharIndex(';', Name, 1)
, 1
From #Table
Union All
Select LTrim(
Case When CharIndex(';', Remained, 1) = 0
Then Remained
Else Left(Remained, CharIndex(';', Remained, 1) - 1)
End
)
, Right(Remained, Len(Remained) - CharIndex(';', Remained, 1))
, CharIndex(';', Remained, 1)
, Level
+ 1
From Result
Where [Index] <> 0
) Select Part
, Count(*)
From Result
Group By Part
Cheers
;with cte as
(
select 1 as Item, 1 as Start, CHARINDEX(';',inventors, 1) as Split, Inventors from YourInventorsTable
union all
select cte.Item+1, cte.Split+1, nullif(CHARINDEX(';',inventors, cte.Split+1),0), inventors as Split
from cte
where cte.Split<>0
)
select rTRIM(lTRIM(SUBSTRING(inventors, start,isnull(split,len(inventors)+1)-start))), count(*)
from cte
group by rTRIM(lTRIM(SUBSTRING(inventors, start,isnull(split,len(inventors)+1)-start)))
You can create a split function to split the col values
select splittedValues.items,count(splittedValues) from table1
cross apply dbo.split(col1,';') splittedValues
group by splittedValues.items
DEMO in Sql fiddle
first make one function who take your comma or any other operator(;) separated string into one table and by using that temp table, apply GROUP function on that table.
So you will get count for separate value.
"select d.number,count(*) from (select number from dbo.CommaseparedListToTable('Hundley;Edward;Ana;Isler;Hunsberger;Hunsberger;Hundley',';'))d
group by d.number"
declare #text nvarchar(255) = 'Edward; Hundley; AnaIsler; Hunsberger; Hunsberger; Hundley ';
declare #table table(id int identity,name varchar(50));
while #text like '%;%'
Begin
insert into #table (name)
select SUBSTRING(#text,1,charindex(';',#text)-1)
set #text = SUBSTRING(#text, charindex(';',#text)+1,LEN(#text))
end
insert into #table (name)
select #text
select name , count(name ) counts from #table group by name
Output
name count
AnaIsler 1
Hundley 2
Hunsberger 2
Edward 1