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 8840145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:23:55+00:00 2026-06-14T10:23:55+00:00

Dear Stackoverflow members I have a recursive CTE query like this: DECLARE @Level TABLE

  • 0

Dear Stackoverflow members

I have a recursive CTE query like this:

DECLARE @Level TABLE (ID int, ParentID int, Name varchar(max))

INSERT INTO @Level (ID, ParentID, Name)
         VALUES (1,0, 'AAAA'),
                (2,1, 'BBBB'),
                (3,2, 'CCCC'),
                (4,3, 'DDDD'),
                (5,4, 'EEEE')

;WITH cte (ID, ParentID, Name, Path, Level) AS
(
     SELECT 
         ID, ParentID, Name, CONVERT(varchar(MAX), Name), 1
     FROM 
         @Level
     WHERE 
         ParentID = 0

     UNION ALL

     SELECT 
         n.ID, n.ParentID, n.Name, 
         CONVERT(varchar(MAX), cte.Path + '/' + n.Name), cte.Level + 1
     FROM 
         @Level n
     JOIN 
         cte on n.ParentID = cte.ID
   )
   SELECT * FROM cte  

Result from above query:

ID          ParentID    Name    Path                         Level
----------- ----------- ------- ---------------------------- -----------
1           0           AAAA    AAAA                         1
2           1           BBBB    AAAA/BBBB                    2
3           2           CCCC    AAAA/BBBB/CCCC               3
4           3           DDDD    AAAA/BBBB/CCCC/DDDD          4
5           4           EEEE    AAAA/BBBB/CCCC/DDDD/EEEE     5

Required output from the CTE query:

ID          ParentID    Name   Paths                        Path                         Level
----------- ----------- ----------------------------------- ---------------------------- -----------
1           0           AAAA   AAAA                         AAAA                         1
2           1           BBBB   AAAA/BBBB                    AAAA/BBBB                    2
3           2           CCCC   AAAA/.../CCCC                AAAA/BBBB/CCCC               3
4           3           DDDD   AAAA/.../.../DDDD            AAAA/BBBB/CCCC/DDDD          4
5           4           EEEE   AAAA/.../.../.../EEEE        AAAA/BBBB/CCCC/DDDD/EEEE     5

As seen from the example above, the path can get quite long and would rather have the path replaced with dots. With the exception that the start and end characters remain visible and only the middle contents replaced. Please note that the contents of VALUES can be anything this is just used as an example.

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-06-14T10:23:57+00:00Added an answer on June 14, 2026 at 10:23 am

    This collapses all except Root and Current

       DECLARE @Level TABLE (ID int, ParentID int, Name varchar(max))
    
            INSERT INTO @Level (ID, ParentID, Name)
             VALUES (1,0, 'AAAA'),
                    (2,1, 'BBBB'),
                    (3,2, 'CCCC'),
                    (4,3, 'DDDD'),
                    (5,4, 'EEEE')
    
    
            ;WITH cte (ID, ParentID, Name, Root, Path, Level) AS
       (
       SELECT ID, ParentID, Name, CONVERT(varchar(MAX), Name),
              CONVERT(varchar(MAX), Name), 1
       FROM @Level
       WHERE ParentID = 0
       UNION ALL
       SELECT n.ID, n.ParentID, n.Name, cte.Root, cte.Root + '/' +
              CASE WHEN cte.Level=1 then ''
                   else replicate('.../', cte.level-1) end +
              n.Name, cte.Level + 1
       FROM @Level n
       JOIN cte on n.ParentID = cte.ID
       )
       SELECT * FROM cte  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

dear all..i have a table it looks like: Name version DDX 01 DTX 05
Hello dear users of the stackoverflow! I really like the search form on this
Dear stackoverflow members, I have a small problem, I want to replace some characters
Dear gods of Stackoverflow Let's say I have a MySQL query that selects a
Dear members of the Stackoverflow community, We are developing a web application using the
Dear all,Now i have this question in my java program,I think it should be
Dear stackoveflow, I have this problem. I'm working with an old version of mssql
dear all..i have this code: <script> var str=KD-R435MUN2D; var matches=str.match(/(EE|[EJU]).*(D)/i); if (matches) { var
Dear Stackoverflow Developers i have to load image as an icon to jmenu from
Hello dear members of stackoverflow I've recently started learning C++, today I wrote a

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.