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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:31:19+00:00 2026-05-10T16:31:19+00:00

I have two tables, Book and Tag , and books are tagged using the

  • 0

I have two tables, Book and Tag, and books are tagged using the association table BookTag. I want to create a report that contains a list of books, and for each book a list of the book’s tags. Tag IDs will suffice, tag names are not necessary.

Example:

Book table: Book ID | Book Name 28      | Dracula  BookTag table: Book ID | Tag ID 28      | 101 28      | 102 

In my report, I’d like to show that book #28 has the tags 101 and 102:

Book ID | Book Name | Tags 28      | Dracula   | 101, 102 

Is there a way to do this in-line, without having to resort to functions or stored procedures? I am using SQL Server 2005.

Please note that the same question already has been asked in Combine multiple results in a subquery into a single comma-separated value, but the solution involves creating a function. I am asking if there is a way to solve this without having to create a function or a stored procedure.

  • 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. 2026-05-10T16:31:20+00:00Added an answer on May 10, 2026 at 4:31 pm

    You can almost do it. The only problem I haven’t resolved is the comma delimiter. Here is a query on a similar structure that separates the tags using a space.

    SELECT em.Code,     (SELECT et.Name + ' '  AS 'data()'         FROM tblEmployeeTag et             JOIN tblEmployeeTagAssignment eta ON et.Id = eta.EmployeeTag_Id AND eta.Employee_Id = em.id     FOR XML PATH('') ) AS Tags FROM tblEmployee em 

    Edit:

    Here is the complete version using your tables and using a comma delimiter:

    SELECT bk.Id AS BookId,         bk.Name AS BookName,     REPLACE((SELECT LTRIM(STR(bt.TagId)) + ', '  AS 'data()'         FROM BookTag bt         WHERE bt.BookId = bk.Id                  FOR XML PATH('') ) + 'x', ', x','') AS Tags FROM Book bk 

    I suppose for future reference I should explain a bit about what is going on. The ‘data()’ column name is a special value that is related to the FOR XML PATH statement. It causes the XML document to be rendered as if you did an .InnerText on the root node of the resulting XML.
    The REPLACE statement is a trick to remove the trailing comma. By appending a unique character (I randomly chose ‘x’) to the end of the tag list I can search for comma-space-character and replace it with an empty string. That allows me to chop off just the last comma. This assumes that you are never going to have that sequence of characters in your tags.

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

Related Questions

Loading...

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could Embed Windows Media Player on a Form. UPDATE:… May 11, 2026 at 7:35 am
  • added an answer something like this: a[b[@type='foo'][@value='1']] should do the trick May 11, 2026 at 7:35 am
  • added an answer EDIT: My first attempt didn't work, but this one does.… May 11, 2026 at 7:35 am

Top Members

Trending Tags

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

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.