Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
546 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
A table-valued function in SQL to split a comma-delimited list |
---|
If you're creating Reporting Services reports using multivalue parameters and stored procedures, you'll find this function to split a list of values into a single-table column useful. |
In this blog
There is now a built-in function called String_Split within SQL to do this, so if you're using SQL Server version 2016 you may be able to disregard the rest of this page.
Suppose you have a string of values, separated by commas, and you want to turn them into a single-column table. For example, you want to take an input like this:
And turn it into this:
The single-column table of values you want to produce from the comma-delimited string of text given.
This blog gives an SQL table-valued function to do this, which you are welcome to copy and paste!
I've written this blog as a support to our manual chapter on using multivalue parameters in SSRS reports, although it's possible it may have more general use.
Here's one way to achieve this:
CREATE FUNCTION fnSplitIds(
@ids varchar(max)
)
-- function returns a single-column table of IdValue numbers
RETURNS @IdValues TABLE(
IdValue varchar(10)
)
BEGIN
-- get rid of any spaces (shouldn't be any)
SET @ids = REPLACE(@ids,' ', '')
-- if no ids passed, exit
IF len(@ids) = 0 RETURN
-- where to start looking ...
DECLARE @start int = 1
-- position of first comma found
DECLARE @end int = CHARINDEX(',', @ids)
-- if no comma, just return the one value
IF @end = 0
BEGIN
INSERT INTO @IdValues(IdValue) VALUES (@ids)
RETURN
END
-- keep looking till no more commas
WHILE @end > 0
BEGIN
-- add this row
INSERT INTO @IdValues (IdValue) VALUES (
SUBSTRING(@ids, @start, @end - @start)
)
-- go on to next comma
SET @start = @end + 1
SET @end = CHARINDEX(',', @ids, @start)
END
-- add final row
INSERT INTO @IdValues (IdValue) VALUES (
SUBSTRING(@ids, @start, LEN(@ids) - @start + 1)
)
RETURN
END
When you run this code, you should get a new table-valued function in your database:
You may need to right-click on your list of table-valued functions to refresh it, to make your new function appear in the list.
To check the new function works, create a new query in the same database and try running some checks:
-- try with more than 2 values
select * from dbo.fnSplitIds('44,222,28')
-- try with no values
select * from dbo.fnSplitIds('')
-- try with one value
select * from dbo.fnSplitIds('WiseOwl')
Here's what this should show:
The SQL commands should show 3, 0 and 1 rows respectively.
You can now call your function from any other SQL query!
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.