You can use the below steps:-
1. Create a table Value and store all values to this table.
2. Below are the different queries:-
Query 1: Using "COALESCE" function
DECLARE @temp VARCHAR(MAX)
SELECT @temp = COALESCE(@temp+', ' ,'') + val
FROM [dbo].[Value]
SELECT @temp
Query 2: Using without "COALESCE" function
DECLARE @temp VARCHAR(MAX)
SET @temp = ''
SELECT @temp = @temp + val + ', '
FROM [dbo].[Value]
SELECT SUBSTRING(@temp, 0, LEN(@temp))
Query 3: Using "FOR XML PATH"
DECLARE @temp VARCHAR(MAX)
SET @temp = (SELECT ', ' + cast(s.val as varchar)
FROM [dbo].[Value] s
ORDER BY s.state
FOR XML PATH(''))
SELECT SUBSTRING(@temp, 2, 200000) AS state
Hope this will help you.
Murugavelmsc, if this helps please login to Mark As Answer. | Alert Moderator