Avatar of conrad2010
conrad2010
 asked on

T-SQL select output to string

what is the quickest way to have the resultset (multimple records, one field) of a SELECT statement be formatted into one string?
SELECT
   name
FROM
   tblCity
WHERE
   state = 'CA'

Open in new window

Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
Reza Rad

8/22/2022 - Mon
Brad Howe

Can you clarify what you are trying to do?

I'm guess, you want the select multiple fields from a conditional select statement and have the returned result 1 string but your code snippet is confusing.

Let us know.
ASKER CERTIFIED SOLUTION
Reza Rad

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Reza Rad

another way:

SELECT 
                name+',' AS cities 
            FROM 
                tblCity
			WHERE
				state = 'CA'
            FOR XML PATH ('')

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes