acctcode primekey groupby <--- columns
WDS 1 'NULL' <--- values (varchar)
FDS 2 'NULL'
IRN 3 'NULL'
SUM 4 1,2,3
STL 5 'NULL'
WTR 6 'NULL'
SUM2 7 5,6
I want to split string groupby column where values are NOT EQUAL to 'NULL'
and save it to another table which will look like this:
acctcode primekey groupby
SUM 4 1
SUM 4 2
SUM 4 3
SUM2 7 5
SUM2 7 6
here is my split string code:
ALTER FUNCTION [dbo].[SplitStrings]
(
@List NVARCHAR(MAX),
@Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT primekey = y.i.value('(./text())[1]', 'nvarchar(4000)')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(@List, @Delimiter, '</i><i>')
+ '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
);
here is the code I use to call the function:
SELECT acctcode,(SELECT * FROM SplitStrings(groupby,','))as prime
INTO Chadtblsum
FROM Chadothercharges WHERE acctcode = acctcode
The code above gives me this error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.