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

  • Home
  • SEARCH
  • 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 8750771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:29+00:00 2026-06-13T12:55:29+00:00

I am looking to return a query that shows exactly like this: Root 1

  • 0

I am looking to return a query that shows exactly like this:

Root 1
–> Child 2
—–>Child 3
Root 2
–> Child 4
—->Child 5

So the query should return Root 1 as One row, —> Child 2 as another row. Assume n Levels, and “—>” format is placed for each child. Level is higher then “—->” increases.

My Table definition is

[NodeId, ParentId, Name, Level]

  • 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-06-13T12:55:30+00:00Added an answer on June 13, 2026 at 12:55 pm

    On SQL Server 2008 and above, you can use hierarchyId datatype to quickly achieve the desired sorting. You can use REPLICATE() to get the dashes.

    ;with cte as (
        select NodeId, ParentId, Name, 0 Level, '/' + cast(NodeId as varchar(max)) + '/' Hier
        from tbl1
        where ParentId is null
        union all
        select t.NodeId, t.ParentId, t.Name, Level+1, Hier + cast(t.NodeId as varchar(max)) + '/'
        from tbl1 t
        join cte c on t.ParentId = c.NodeId
    )
    select case when level=0
                then ''
                else replicate('-',level*2) + '>' end + Name
    from cte
    order by cast(Hier as hierarchyid);
    

    SQL Fiddle

    On earlier SQL Server 2005, you can emulate the hierarchyId sorting using zero-padded strings:

    ;with cte as (
        select NodeId, ParentId, Name, 0 Level, right(replicate('0',10)+cast(NodeId as varchar(max)),11) Hier
        from tbl1
        where ParentId is null
        union all
        select t.NodeId, t.ParentId, t.Name, Level+1, Hier + right(replicate('0',10)+cast(t.NodeId as varchar(max)),11)
        from tbl1 t
        join cte c on t.ParentId = c.NodeId
    )
    select case when level=0
                then ''
                else replicate('-',level*2) + '>' end + Name
    from cte
    order by Hier;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm constructing a regex that is looking for dates. I would like to return
Not sure this is possible, but looking to write a script that would return
The below example shows how I'm building the query string that will return a
I'm looking to run a query that will return a list of columns in
I'm looking to create a query that will return of list of each category
I am looking for an SQL query that will return a 1 or a
I'm looking for a way to query a SQL database with Java and return
I was just looking at this code and i don't understand what RETURN TRUE
I'm looking for an implementation of java.util.Map that has a method that will return
I'm looking for a way to atomically increment a short, and then return that

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.