Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favourite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved 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: 3504 | Views in the last 30 days: 12
 
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

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones