Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Tree table with three Columns (like user_id,advisor_Id,Tree).
Table Name:tbl_treeIntro
My table records are like this,
SQL
user_id      advisor_Id     Tree
7               3           3,1,0
9               4           4,1,0
12              5           5,2,0

I have tried split function,which i have refered from google.
My Split fuction script is here,
SQL
CREATE FUNCTION [dbo].[Split]
(
    @String NVARCHAR(4000),
    @Delimiter NCHAR(1)
)
RETURNS TABLE 
AS
RETURN 
(
    WITH Split(stpos,endpos) 
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM Split
)
GO

And Query i have tried is,
SQL
declare @string nvarchar(max);
select @string=Tree from tbl_treeIntro;
SELECT * FROM dbo.Split(@string, ',') 

But i showing result for only single userId.
This is my output,
SQL
ID       Data
1         5
2         2
3         0

But i need results for all userId.
My Expecting Output:
SQL
ID       Data       UserId
1         5           12
2         2           12
3         0           12
4         4           9
5         1           9
6         0           9
7         3           7
8         1           7
9         0           7


Here i have provided reference table script and table values.
SQL
CREATE TABLE [dbo].[tbl_treeIntro](
	[t_id] [int] IDENTITY(1,1) NOT NULL,
	[user_id] [bigint] NULL,
	[advisor_id] [bigint] NULL,
	[Tree] [nvarchar](max) NULL,
 CONSTRAINT [PK_tbl_treeIntro] PRIMARY KEY CLUSTERED 
(
	[t_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

insert into tbl_treeIntro(user_id,advisor_id,Tree) values(7,3,'3,1,0')
insert into tbl_treeIntro(user_id,advisor_id,Tree) values(9,4,'4,1,0')
insert into tbl_treeIntro(user_id,advisor_id,Tree) values(12,5,'5,2,0')

Thanks in Advance,
R@JEESH
Posted
Updated 6-Mar-15 2:29am
v2

Tested in SQL SERVER 2008 and worked.

SQL
DECLARE @String NVARCHAR(MAX)
SET @String=''

SELECT  @String = @String + replace(Tree+',' ,',', '$'+cast(User_Id AS varchar) +',')
FROM    tbl_treeIntro

SELECT  @String = LEFT(@String,LEN(@String)-1)

SELECT * FROM dbo.Split(@string, ',')


ALTER FUNCTION [dbo].[Split]
(
    @String NVARCHAR(4000),
    @Delimiter NCHAR(1)
)
RETURNS TABLE 
AS


RETURN 
(
    
    WITH Split(stpos,endpos) 
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' =  SUBSTRING(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos),1,CHARINDEX('$',SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos))-1),
                'UserId' =  SUBSTRING(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos),CHARINDEX('$',SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos))+1,LEN(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)))

    FROM Split
   
)
GO


The output is

Id	Data	UserId
1	3	     7
2	1	     7
3	0	     7
4	4	     9
5	1	     9
6	0	     9
7	5	     12
8	2	     12
9	0	     12
 
Share this answer
 
v3
Comments
King Fisher 6-Mar-15 12:10pm    
you got missed UserId !!!
John C Rayan 6-Mar-15 17:34pm    
Check Now
King Fisher 6-Mar-15 23:38pm    
Good Play my 5+ ;)
Rajesh waran 6-Mar-15 23:41pm    
5*.
Rajesh waran 6-Mar-15 23:40pm    
Thanks it was working. But got message like this "The statement terminated. The maximum recursion 100 has been exhausted before statement completion". So i have added a line at last like "option(maxrecursion 1000)" Now it's working fine. Thanks for the reply.
Check this out Converting comma separated data in a column to rows for selection[^]
You will have to add ORDER BY and ROW_NUMBER() to get the order and ID that you want.
Refer: How to use ROW_NUMBER() to enumerate and partition records in SQL Server[^]
 
Share this answer
 
Comments
King Fisher 6-Mar-15 23:39pm    
Good links my 5.
Peter Leow 6-Mar-15 23:58pm    
Thank you, King Fisher.
Rajesh waran 6-Mar-15 23:47pm    
Thank you for the wounderful links, I have referred Griff's tips/trics, and it was very helpful to me. I got solution from Jofn C Rayan with little modifications.
Peter Leow 6-Mar-15 23:59pm    
You are welcome.
John C Rayan 7-Mar-15 8:46am    
Thanks King Fisher for helping out Rajesh. Also thanks to Peter for the useful links.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900