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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:56:49+00:00 2026-06-18T11:56:49+00:00

I have the following tables in a SQL server Database: [ANIMALS] [COLORS] ID |

  • 0

I have the following tables in a SQL server Database:

[ANIMALS]            [COLORS]
ID | NAME            ID | NAME
 1   Cow              1   Blue
 2   Chicken          2   Red
 3   Pig              3   Green
 4   T-Rex            4   Pink
                      5   Orange

  [RELATIONSHIPS]         
ANIMAL ID | COLOR ID  
    1          3
    1          2
    1          5
    2          1
    3          1
    3          4
    4          1
    4          2
    4          5

I am trying to write a SQL query that will return the the list of animals along with each of their respective colors. The goal I am attempting to achieve will look like the following:

  ANIMAL NAME     COLORS
     Cow            Green/ Red / Orange
     Chicken        Blue
     Pig            Blue/ Pink
     T-Rex          Blue/ Red/ Orange              

I already have a user defined function that will create the delimited string (“color/ color/ etc.”) called udf_Delimit_Colors(@animalID) — @animalID is the only parameter within that UDF.

I can return only one row by passing in an animalID and am now stumped as to how I would go about achieving my goal without passing in any ID.

The delimited SQL Query can be found at the following link:
https://sites.google.com/site/sqlblindman/creatingcomma-delimitedstrings

+rep to the person that wrote it.

I am still working on it too so if i figure it out I will post the answer.
Thanks in advance for your input.

  • 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-18T11:56:50+00:00Added an answer on June 18, 2026 at 11:56 am

    Based on your syntax above, I am assuming SQL Server as the database. You can concatenate the string the following ways:

    SELECT a.name,
      LEFT(colors , LEN(colors)-1) colors 
    FROM animals a
    CROSS APPLY
    (
        SELECT c.name + ' / '
        FROM colors c
        left join relationships r
          on c.id = r.[COLOR ID]
        where a.id = r.[ANIMAL ID]
        FOR XML PATH('')
    ) m (colors)
    

    See SQL Fiddle with Demo

    Or:

    select a.name,
      STUFF((SELECT distinct ' / ' + c.name
             from colors c
             left join relationships r
               on c.id = r.[COLOR ID]
             where a.id = r.[ANIMAL ID]
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,2,'') colors
    from animals a;
    

    See SQL Fiddle with Demo

    The result is:

    |    NAME |                COLORS |
    -----------------------------------
    |     Cow | Red / Green / Orange  |
    | Chicken |                 Blue  |
    |     Pig |          Blue / Pink  |
    |   T-Rex |  Blue / Red / Orange  |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables in SQL Server 2005 ReceiptPoint: ID (PK), Name GasIndexLocation:
I have Following Tables in SQL Server Database: (1) StudentMaster (StudentId, StudentName) (2) SubjectMaster
I have the following tables in a SQL Server 2000 database: Master MasterID |
I have the following XML generated from various tables in my SQL SERVER database
I have the following 2 tables defined in a SQL Server database: CREATE TABLE
I have a following scenario in my SQL Server 2005 database. zipcodes table has
i have the following tables in sql server: photoalbumsTable: album_ID album_caption albumtagmap id album_id
I have two tables in a SQL Server 2008 database in my company. The
In a SQL Server database, I have two tables, s and t. A further
I have two SQL tables in a SQL Server 2008 database that look like

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.