Split Delimited Column into Multiple Rows....

I have table

CREATE TABLE [dbo].[stager](
      [pid] [varchar](10) NULL,
      [event_type] [varchar](10) NULL,
      [comments] [varchar](250) NULL,
      [staff_id] [varchar](10) NULL

SELECT TOP 1000 [pid]
  FROM [NCS_VDRSTAGE].[dbo].[stager]

returning values

pid      event_type      staff_id
00009      13; 14      36261
00009      24; 25      36261
00009      13; 14      36261
00032      13; 14      36261
00032      15; 16      36261
00051      13; 14      36261
00051      15; 16      36261
00070      13; 14      36261
00090      13; 14      36261
00090      15; 16      36261
00091      13; 14      36261
00091      15; 16      36261
00118      13; 14      36261
00118      15; 16      36261

How can I split this to look like

00009      13      36261
00009      14      36261
00009      24      36261
00009      25      36261
00009      13      36261
00009      14      36261
00032      13      36261
00032      14      36261
00032      15      36261
00032      16      36261
I have used the Split function

CREATE FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))    
   returns @temptable TABLE (items varchar(8000))      
       declare @idx int      
        declare @slice varchar(8000)      

        select @idx = 1      
            if len(@String)<1 or @String is null  return      

       while @idx!= 0      
           set @idx = charindex(@Delimiter,@String)      
           if @idx!=0      
               set @slice = left(@String,@idx - 1)      
              set @slice = @String      

               insert into @temptable(Items) values(@slice)      

           set @String = right(@String,len(@String) - @idx)      
           if len(@String) = 0 break      

but doing this

select dbo.Split(event_type,'; ') from  [NCS_VDRSTAGE].[dbo].[stager]

is resulting in

Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.Split", or the name is ambiguous.

I have used it with and without dbo and several other manipulations but it did not work. I am at a loss. I am amenable to non sql server solutions if you have one....


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.

Try this form.

Also, you really need an order by clause within the CTE for the TOP 1000 to be meaningfull
With myCTE as
(SELECT TOP 1000 [pid]
      ,substr([event_type],1,instr([event_type]+';',';')-1) type_one
      ,substr([event_type],instr([event_type]+';',';')+1,len([event_type)-instr([event_type]+';',';') type_two
  FROM [NCS_VDRSTAGE].[dbo].[stager]
select pid, type_one, staff_id from myCTE
select pid, type_two, staff_id from myCTE
order by pid

Open in new window


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
The error you're getting is because your trying to use a table function as a scalar one. To use your function, treat it like another table

select * from  [NCS_VDRSTAGE].[dbo].[stager] inner join dbo.Split(event_type,'; ') on ...

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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.