Header Ads

Retrieving SQL select table rows as a single row comma separated

Try Following Query to retrieving SQL select table rows as a single row comma separated.

DECLARE @vResult VARCHAR(MAX)

SELECT @vResult = COALESCE(@vResult + ',', '') + CAST([Column Name] AS VARCHAR)
FROM [Table Name]

SELECT @vResult

Eg:

Select LastName from tblUsers

Above Query Will returns:

Srikanth
Gupta
Parmesh


DECLARE @vResult VARCHAR(MAX)

SELECT @vResult = COALESCE(@vResult + ',', '') + CAST(LastName AS VARCHAR)
FROM [Table Name] tblUsers

SELECT @vResult

Above Query Will returns:

Srikanth,Gupta,Parmesh

Happy Coding!!!

No comments:

Powered by Blogger.