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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:48:50+00:00 2026-06-04T10:48:50+00:00

CatID parID catName 1 -1 A 2 1 B 3 2 C 4 3

  • 0
CatID  parID  catName
1      -1        A
2       1        B  
3       2        C
4       3        D

I want to write a query which returns the parent child relationship in string format.

In the above table the catName has parentId -1, which means it has got no parent. B has parentID 1, which means that A is its parent.

So finaly the string is like this

A=>B=>c=>D

This is the way I want to generate a query.

I’ll pass CatID, and it will traverse until it gets a -1.

  • 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-04T10:48:51+00:00Added an answer on June 4, 2026 at 10:48 am
    declare @CatID int;
    set @CatID = 4;
    
    with C as
    (
      select parID,
             cast(catName as varchar(max)) as catName
      from YourTable
      where CatID = @CatID
      union all
      select T.parID,
             T.catName + '=>' + C.catName
      from YourTable as T
        inner join C
          on T.CatID = C.parID
    )
    select catName
    from C
    where parID = -1
    

    SE-Data

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

Sidebar

Related Questions

I have designed a Class for Parent Child relationship class Category { public string
I have the following tables: TABLE: category catid catfatherid catname 1 0 United States
I want to execute following query: from Item i where i.categoryItems.catalogId = :catId That
I've a database table, which has the following form catID | category | parentID
I have a database table named 'favoritecats' with the following fields: id catName catId
I have a categories table with a father-son relationship which looks something like this:
I have a table of categories: Catid | Desciption 1 | Color 2 |
I have the following SQL: ALTER PROCEDURE [dbo].[SP_Products_GetList] @CatID int, @CatName int, @SortBy varchar(50),
I have a page (ie www.mysite.com/products.php?catid=5 ) in which I use the $_REQUEST[catid'] to
I have a table like this: ID CatID Filename 1 1 abc 2 2

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.