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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:34:39+00:00 2026-05-12T11:34:39+00:00

Greetings, fellow coders! I have this table that contains categories and subcategories (actually I

  • 0

Greetings, fellow coders!

I have this table that contains categories and subcategories (actually I don’t, but let’s stick to a classic example):

Id     ParentId     Name
1      NULL         A
2      1            B
3      2            C
4      3            D

5      NULL         B
6      5            D

Is there a way for me to get category “D” (id 4) by querying the table for the full path? (see pseudocode below)

SELECT * FROM Categories WHERE FullPath = "A/B/C/D"

// Result:
Id     ParentId     Name
4      3            D

I know that it’s possible to use left joins to get the full path, but how do I write queries to get the leaf node by providing a path?

EDIT (Solution):

Help the help from both van and Eric, this is what I did:

with p as
(
    select
        c.*,
        cast(c.Name as varchar(1024)) as NamePath
    from
        Categories c
    where 
        ParentCategoryId is null
    union all
    select
        c.*,
        cast(p.NamePath + '/' + c.Name as varchar(1024)) as NamePath
    from
        Categories c
        inner join p on
            c.ParentCategoryId = p.CategoryId
)
select Id, Name
from p
where NamePath = 'A/B/C/D'

Thanks guys, both your answers were very helpful! I wish that I was able to mark them both as the solution.

This time I will simply go for the one with the leasts ammount of points (that is van).

  • 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-12T11:34:39+00:00Added an answer on May 12, 2026 at 11:34 am

    With the help of CTE:

    WITH CategoriesWithPath (id, parentid, name, path) AS
    (
        SELECT  c.*, cast(c.name AS VARCHAR(1024)) AS "path"
        FROM    @Categories c
        WHERE   parentid is null
        UNION ALL
        SELECT  c.*, cast(p."path" + '/' + c.name AS VARCHAR(1024)) AS "path"
        FROM    @Categories c
        INNER JOIN CategoriesWithPath p ON c.parentid = p.id
    )
    SELECT  id, parentid, name 
    FROM    CategoriesWithPath
    WHERE   "path" = 'a/b/c/d'
    

    But I would create a view using this CTA, so that you can easily just execute a SELECT from this view based on “path” filter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I've read some threads about this topic, but actually I was unable to
Greetings I may have imagined this but does anyone know if Last.fm previously used
Greetings fellow amazonians We have certain f4v files from webtvinteractive.com that are not working
Greetings! I have a Repeater control that's using an XmlDataSource control. <asp:FormView id=myFormView runat=server
Greetings, My app name(the one that appears on the home screen) contains a special
Greetings everyone I have this regular expression which goes as follow: $thread_views_exp = '~<td
Greetings fellow stackoverflowers, I'm working on a jQuery class where I have a function
Greetings, I would like to use EF4 but I only have Visual studio 2010
Greetings to all! This is my first question here on stackoverflow. I have a
Greetings fellow earthlings, I am trying to customize a popup box. I don't want

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.