Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 852185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:33:12+00:00 2026-05-15T07:33:12+00:00

Based on the following table ID Path ————————————— 1 \\Root 2 \\Root\Node0 3 \\Root\Node0\Node1

  • 0

Based on the following table

ID    Path       
---------------------------------------
1  \\Root
2  \\Root\Node0
3  \\Root\Node0\Node1
4  \\Root\Node0\Node2
5  \\Root\Node3
6  \\Root\Node3\Node4
7  \\Root\Node5
...
N  \\Root\Node5\Node6\Node7\Node8\Node9\Node10

so on…

There are around 1000 rows in this table. I want to display individual nodes in separate columns. Maximum columns to be displayed 5 (i.e. node till 5 level deep). So the output
will look as below

ID    Path           Level 0   Level 1  Level 2  Level 3  Level 4  Level 5
----------------------------------------------------------------------------------------
1  \\Root                    Root      Null     Null     Null     Null     Null
2  \\Root\Node0              Root      Node 0   Null     Null     Null     Null
3  \\Root\Node0\Node1        Root      Node 0   Node 1   Null     Null     Null
4  \\Root\Node0\Node2        Root      Node 0   Node 2   Null     Null     Null
5  \\Root\Node3              Root      Node 3   Null     Null     Null     Null
6  \\Root\Node3\Node4        Root      Node 3   Node 4   Null     Null     Null
7  \\Root\Node5              Root      Node 5   Null     Null     Null     Null
...
N  (see in above table)      Root      Node 5   Node 6   Node 7   Node 8 Node 9

The only way I can think of is to open a cursor, loop through each row and perform a string split, just fetch the first 5 nodes and then insert into a temp table.

Please suggest.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:33:12+00:00Added an answer on May 15, 2026 at 7:33 am

    What you need is a table-valued split function akin to the following:

    CREATE FUNCTION [dbo].[udf_Split] (@DelimitedList nvarchar(max), @Delimiter nvarchar(2) = ',')
    RETURNS @SplitResults TABLE (Position int NOT NULL PRIMARY KEY, Value nvarchar(max))
    AS
    Begin
        Declare @DelimiterLength int
        Set @DelimiterLength = DataLength(@Delimiter) / 2
    
        If Left(@DelimitedList, @DelimiterLength) <> @Delimiter
            Set @DelimitedList = @Delimiter + @DelimitedList
    
        If Right(@DelimitedList, @DelimiterLength) <> @Delimiter
            Set @DelimitedList = @DelimitedList + @Delimiter
    
        Insert @SplitResults(Position, Value)
        Select CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength            
            , Substring (
                        A.List
                        , CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength         
                        , CharIndex(@Delimiter, A.list, N.Value + 1)                            
                            - ( CharIndex(@Delimiter, A.list, N.Value) + @DelimiterLength ) 
                        )
        From dbo.Numbers As N
            Cross Join (Select @DelimitedList As list) As A
        Where N.Value > 0
            And N.Value < LEN(A.list)
            And Substring(A.list, N.Value, @DelimiterLength) = @Delimiter
        Order By N.Value
    
        Return
    End
    

    This function relies on the existence of a Numbers table which contains a sequential list of integers. Now, you could take your original data and do something like:

    With TableData As
        (
        Select 1 As Id, '\\Root' As [Path]
        Union Select All 2, '\\Root\Node0'
        Union Select All 3, '\\Root\Node0\Node1'
        Union Select All 4, '\\Root\Node0\Node2'
        Union Select All 5, '\\Root\Node3'
        Union Select All 6, '\\Root\Node3\Node4'
        Union Select All 7, '\\Root\Node5'
        )
        , SplitData As
        (
        Select T.Id, T.[Path], S.Value
            , Row_Number() Over ( Partition By T.Id Order By S.Position ) As Level
        From TableData As T
            Cross Apply dbo.udf_Split( (Substring(T.[Path],2,Len(T.[Path])) + '\') , '\') As S
        )
    Select Id, [Path]
        , Min( Case When Level = 1 Then S.Value End ) As Level0
        , Min( Case When Level = 2 Then S.Value End ) As Level1
        , Min( Case When Level = 3 Then S.Value End ) As Level2
        , Min( Case When Level = 4 Then S.Value End ) As Level3
        , Min( Case When Level = 5 Then S.Value End ) As Level4
    From SplitData As S
    Group By Id, [Path]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer COM+ was Microsoft's offering in the battle for the middle… May 16, 2026 at 10:59 am
  • Editorial Team
    Editorial Team added an answer The max limit is 2GB(1 billion unicode characters) so unless… May 16, 2026 at 10:59 am
  • Editorial Team
    Editorial Team added an answer They are tied to the bean that creates them. EJB… May 16, 2026 at 10:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Based on following table (I have kept spaces between the rows for clarity) Path
Based on an existing table I used CTE recursive query to come up with
I have a table which has following fields project_name VARCHAR (100) running_frequency enum ('daily',
The following SQL code creates a two-line table with an invoice on one line
I have the following records depicting shifts in the Shifts table. ID, Description, Start
I asked this question earlier, but I changed the table structure based on recommendations.
Following situation: Parent: namespace Base; /** @Entity @Table(name=section) */ class Section extends Skeleton {
I have a table where the data are like Data a b c I
This code basically translates characters based on position in one string to the character
I'm currently exporting a database table with huge data (100000+ records) into an xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.