I have a function to split comma delimited strings. It returns a table.
I can use the code fine doing the following:
SELECT * FROM SplitBigInt('100,200,300,400')
This will give me a table with 4 rows.
But I can't figure out how to use the function with a table that has a column that is a comma delimited string.
It would return n-tables. I want one table with all the values in one column in one table. Just like the above SELECT does.
How would I do my select to select all these rows and end up with a one column table with one of these values in each column. For example:
AList
80
15
200
80
200
250
80
etc.
Thanks,
Tom
Code:
CREATE FUNCTION dbo.SplitBigInt ( @strString varchar(4000))
RETURNS @Result TABLE(Value bigint)
AS
begin
WITH StrCTE(start, stop) AS
(
SELECT 1, CHARINDEX(',' , @strString )
UNION ALL
SELECT stop + 1, CHARINDEX(',' ,@strString , stop + 1)
FROM StrCTE
WHERE stop > 0
)
insert into @Result
SELECT SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS stringValue
FROM StrCTE
return
end
I can use the code fine doing the following:
SELECT * FROM SplitBigInt('100,200,300,400')
This will give me a table with 4 rows.
But I can't figure out how to use the function with a table that has a column that is a comma delimited string.
It would return n-tables. I want one table with all the values in one column in one table. Just like the above SELECT does.
Code:
If OBJECT_ID('tempdb..#SomeLists') IS NOT NULL
DROP TABLE #SomeLists
Create Table #SomeLists
(
ID int,
AList varchar(100)
)
INSERT #SomeLists (ID, AList) VALUES(1, '80,15,200,80')
INSERT #SomeLists (ID, AList) VALUES(2, '200,250,80,920')
INSERT #SomeLists (ID, AList) VALUES(3, '800,850,22,100')
INSERT #SomeLists (ID, AList) VALUES(4, '900,200,150')
SELECT * from #SomeLists
How would I do my select to select all these rows and end up with a one column table with one of these values in each column. For example:
AList
80
15
200
80
200
250
80
etc.
Thanks,
Tom