Get Values of a Column in a Single Variable

Posted by Lakhangarg under Sql Server category on | Views : 2384
Get all the values of a single column in a variable separated by comma.
DECLARE a Variable and set the Size of the variable according to your requirement. here in this code i have mentioned MAX.

Now Using :
SELECT @TempString = @TempString + CityName +',' FROM Tbl_City

Statement we can append all the values in @TempString separated by comma.

@TempString contains a extra comma at the end. so we need to remove that with the help of :

Sample Code:

SET @TempString = ''
SELECT @TempString = @TempString + CityName +',' FROM Tbl_City
SELECT SUBSTRING(@TempString,0,LEN(@TempString))

Comments or Responses

Login to post response