Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Display multiple rows values as single row value

By Bharat Panthee,

As in the the query if there is the table named #Table1 and have column FirstName.

Lets say there are 4 records in table having specified record in a script query.

If you want to those 4 rows value separated by any delimeter, in my example it is ' ; '

and assign into any variable, here variable is @listValues .

and do the following query:

 

SELECT @listValues = ISNULL(
@listValues + @delimeter + CAST(FirstName AS VARCHAR(8000)),
CAST(FirstName AS VARCHAR(8000))
)
FROM #Table1
ORDER BY FirstName

 

and then if you run the query

 

SELECT list = @listValues

 

you will get the result:

John ; Michiel ; Peter ; Smith

 

Total article views: 3571 | Views in the last 30 days: 3
 
Related Articles
FORUM

how to devied fullname into firstname and lastname

how to devied fullname into firstname and lastname

FORUM

Lastname, Firstname switch

I dont know if this is any help but this is the code I started with: SELECT HOST9006.DESCRIPTION,...

FORUM

query

query

FORUM

Query

Query

FORUM

query

query

 
Contribute