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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:30:16+00:00 2026-05-20T10:30:16+00:00

I am using SQL to concatenate strings together. This statement works: DECLARE @FirstNamesString nvarchar(256)

  • 0

I am using SQL to concatenate strings together.

This statement works:

DECLARE @FirstNamesString nvarchar(256)
SELECT
    @FirstNamesString = COALESCE(@FirstNamesString + ', ', '') + p.FirstName 
FROM 
    Person p
ORDER BY
    p.SortOrder

And I get the list of first names like:

Name1, Name2, Name3

Now I want to add in a possibly null last name field for each of these persons. I tried the sql below but I only get the last item in the list (Name3):

DECLARE @FirstNamesString nvarchar(256)
SELECT @FirstNamesString = COALESCE(@FirstNamesString + ', ', '') + p.FirstName + ISNULL(' ' + p.LastName, '') 
FROM 
    Person p
ORDER BY
    p.SortOrder

BUT if I first insert all of these names into a temp table, then everything works as expected:

CREATE TABLE #Person2
(
     FirstName nvarchar(128) NOT NULL
    ,LastName nvarchar(256) NULL
    ,SortOrder int NOT NULL
)

INSERT INTO #Person2 (FirstName, LastName, SortOrder) (
    SELECT p.FirstName, p.LastName, p.SortOrder FROM Person p)

DECLARE @FirstNamesString nvarchar(256)

SELECT @FirstNamesString = COALESCE(@FirstNamesString + ', ', '') + p.FirstName + ISNULL(' ' + p.LastName, '') 
FROM 
    #Person2 p
ORDER BY
    p.SortOrder

DROP TABLE #Person2

So what is the difference between the statement above against the original table and the one against the temp table? The temp table route works and I am going to use that but I am very curious… what am I missing here?

UPDATED WITH POSSIBLE SOLUTION

See @Martin’s answer but the concatenation of strings isn’t guaranteed to work. So a solution is to insert the last name into the temp table first prior to selecting the list of first names:

CREATE TABLE #Person2
(
     FirstName nvarchar(256) NOT NULL
    ,SortOrder int NOT NULL
)

INSERT INTO #Person2 (FirstName, SortOrder) (
    SELECT p.FirstName + ISNULL(' ' + p.LastName, ''), p.SortOrder
    FROM Person p

)

DECLARE @FirstNamesString nvarchar(256)

SELECT
    @FirstNamesString = COALESCE(@FirstNamesString + ', ', '') + p.FirstName  
FROM 
    #Person2 p
ORDER BY
    p.SortOrder ASC
  • 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-20T10:30:17+00:00Added an answer on May 20, 2026 at 10:30 am

    This approach to concatenate strings isn’t guaranteed to work. Microsoft say “The correct behavior for an aggregate concatenation query is undefined.”. It will fail if the compute scalar ends up in the wrong place in the plan. Can you show both plans?

    You could/should of course just use XML PATH to concatenate strings as you are on SQL Server 2008 and this is documented to work. reliable in current versions.

    Example:

    DECLARE @FirstNamesString nvarchar(256) 
    
    SELECT @FirstNamesString = (
    SELECT CASE
             WHEN ROW_NUMBER() OVER (ORDER BY (p.SortOrder)) = 1 THEN ''
             ELSE ','
           END + p.FirstName + ISNULL(' ' + p.LastName, '')
    FROM   Person p
    ORDER  BY p.SortOrder
    FOR XML PATH(''), TYPE  
    
    ).value('.[1]','nvarchar(256)')
    
    
    PRINT @FirstNamesString 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server, how do I split a string so I can access item
Using sql server 2000, I would like to take my production data and put
Using SQL Server, which is the fastest or best practice method to use for
Using SQL Server 2005 and Management Studio how do I insert a picture into
Is using SQL Express in a production environment a reasonable choice? I looked at
Although using SQL FOR XML EXPLICIT is cumbersome, I find myself using it often.
When using SQL Server Management Studio from SQL Server 2005, I can connect to
I am using SQL Server 2005. I have a table with a text column
I'm using SQL Server 2000 to print out some values from a table using
I'm using SQL Server 2005, and I would like to know how to access

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.