Getting results of a column in comma separated format is often required while one is writing the stored procedure. The comma separated values then can be used in SQL ‘In’ clause to filter the result. Below example will show you how to get values of a column in comma separated format:
Now you want your countries to be in comma separated format like India,Australia,US,UK.
Following snippet will give you the desired result:
DECLARE @SQL nvarchar(MAX)
SELECT @SQL = COALESCE(@SQL + ‘,’,”) + CountryName FROM [dbo].[tblCountry]
The result is a string of characters that can be used further in the code.
Enjoy! Happy Coding!!