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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:24:32+00:00 2026-06-10T20:24:32+00:00

I have a temp table variable that I need to update with something sort

  • 0

I have a temp table variable that I need to update with something sort of like a comma delimited string.

declare @clients table (clientid int, hobbylist char(1000))
declare @hobbies table (clientid int, hobbynumber smallint)

insert into @clients values (3, '')
insert into @clients values (5, '')
insert into @clients values (12, '')

insert into @hobbies values (3, 4)
insert into @hobbies values (3, 5)
insert into @hobbies values (3, 7)
insert into @hobbies values (12, 3)
insert into @hobbies values (12, 7)

So @clients contains:

clientid     hobbylist
---------------------------
3
5
12

And @hobbies contains:

clientid     hobbylist
---------------------------
3            4
3            5
3            7
12           3
12           7

I need to update the @clients table so that it contains:

clientid     hobbylist
---------------------------
3            4;;5;;7
5
12           3;;7

The closest thing that I found was this:
How to concatenate multiple rows?
But I can’t understand how he is getting “one, two, three” looking at the what is marked as the correct answer.

I’m using SQL Server 2008 R2 64. I tried using a cursor but it was slow (and there will be tons of this type of thing in this sp).

What’s the most efficient way to do this?

Edit:

From njk’s suggestion, I tried this:

update c
set hobbylist=Stuff((
            Select  ';;' + cast(hobbynumber as char)
            From    @hobbies h
            Where   h.clientid = c.clientid
            For             XML Path('')
    ), 1, 1, '')
from @clients c

It didn’t give me an error, but the result is so wacked, I can’t find a good way to show it here.

e.g. Hobby list for client 3 looks like this:

;4                             ;;5                             ;;7                                                                                                                               

ALMOST works. Don’t know where the spaces are coming from.

Edit 2.

Duh. I’m using cast. I need to trim the thing. This solution works for me on my server. I’m going to see if I can get ClearLogic’s working, as well, so I can mark it as a correct answer.

  • 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-10T20:24:33+00:00Added an answer on June 10, 2026 at 8:24 pm

    Here you go

           ;WITH CTE AS 
             (SELECT DISTINCT  clientid,hobbylist= STUFF((SELECT ';'+ LTRIM(STR(hobbynumber))
              FROM @hobbies yt
               WHERE yt.clientid = sc.clientid            
             FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
             FROM @hobbies AS sc 
             )
             UPDATE a
             SET a.hobbylist=b.hobbylist 
             FROM @clients AS a
             INNER JOIN CTE AS b
             ON a.clientid = b.clientid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a temp table variable with a bunch of columns: Declare @GearTemp table
I have temp table #xml that have 2 columns: CREATE TABLE #xml ( id
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a table where I have around 1.5 million+ results that I need
I have a bunch of records in a table variable like so: Id ProductId
I have a simple temp-table defined in SQL Server 2008 R2 representing a parent-child
I have several values in a temp table called #tempIQ and I want to
I have written a DB2 query to do the following: Create a temp table
I have 1 million rows in MySql table temp and wish to multiply column
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (

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.