Using split function

I've tried to apply a split string function that returns a user ID to each row based on the link stackoverflow.com/questions/10914576/tsql-split-string, using:

DECLARE
@OrderID int = 1,
@UserID       nvarchar(50) = '22,23,24'

SELECT @OrderID, @UserID FROM dbo.splitstring(@UserID)

But I'm returning:

OrderID      UserID
1              22,23,24
1              22,23,24
1              22,23,24

I'd like to return the below and insert as an INT:

OrderID      UserID
1              22
1              23
1              24

Thanks for your help.
barnescoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
seems your function would be returning a "table of", try this to start:

SELECT * FROM dbo.splitstring(@UserID)

I'd expect to see a single column, and that column heading needs to be used in your select list.
0
PortletPaulfreelancerCommented:
perhaps this will be more precise, assuming the column returned is [name]:
DECLARE
@OrderID int = 1,
@UserID       nvarchar(50) = '22,23,24'

SELECT @OrderID, sp.name as UserID FROM dbo.splitstring(@UserID) as sp

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
barnescoAuthor Commented:
That's pretty clever...Thanks!
0
PortletPaulfreelancerCommented:
no problem! just remember that although it is a function,
it may be treated just like a real table (as long as it returns as "table of")

thanks for the grading, cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.