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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:23:53+00:00 2026-05-20T12:23:53+00:00

I have a sql function that includes this code: DECLARE @CodeNameString varchar(100) SELECT CodeName

  • 0

I have a sql function that includes this code:

DECLARE @CodeNameString varchar(100)

SELECT CodeName FROM AccountCodes ORDER BY Sort

I need to concatenate all results from the select query into CodeNameString.

Obviously a FOREACH loop in C# code would do this, but how do I do it in SQL?

  • 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-20T12:23:53+00:00Added an answer on May 20, 2026 at 12:23 pm

    If you’re on SQL Server 2005 or up, you can use this FOR XML PATH & STUFF trick:

    DECLARE @CodeNameString varchar(100)
    
    SELECT 
       @CodeNameString = STUFF( (SELECT ',' + CodeName 
                                 FROM dbo.AccountCodes 
                                 ORDER BY Sort
                                 FOR XML PATH('')), 
                                1, 1, '')
    

    The FOR XML PATH('') basically concatenates your strings together into one, long XML result (something like ,code1,code2,code3 etc.) and the STUFF puts a “nothing” character at the first character, e.g. wipes out the “superfluous” first comma, to give you the result you’re probably looking for.

    UPDATE: OK – I understand the comments – if your text in the database table already contains characters like <, > or &, then my current solution will in fact encode those into &lt;, &gt;, and &amp;.

    If you have a problem with that XML encoding – then yes, you must look at the solution proposed by @KM which works for those characters, too. One word of warning from me: this approach is a lot more resource and processing intensive – just so you know.

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

Sidebar

Related Questions

I have an MS SQL function that is called with the following syntax: SELECT
I have an user defined table function in SQL Server that aggregate data from
I have this part of code string query = SELECT ID, COL1 FROM TABLE1
I have edited this from my real code, so that it is a little
I have a SQL function that I am calling in my VB.Net code within
I have a c# function that needs to write to a SQL image column.
I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and
I have created a function that takes a SQL command and produces output that
I have this php script that generate an sql query. However, it needs a
I have functions in place that will convert the results of sql queries into

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.