Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create varchar from SELECT result

DECLARE @result varchar(MAX)

SELECT 
  NAME
FROM
  Table
WHERE
  ID = @Id

I need to fill @result with result Names, separated by ','.
For example, if SELECT returns 'A','B' and 'C', the @result should be 'A, B, C'.
It is Microsoft SQL Server 2008.

like image 647
Sergey Metlov Avatar asked Dec 09 '22 08:12

Sergey Metlov


1 Answers

DECLARE @result varchar(MAX)
SET @result = '';

SELECT 
  @result = @result + NAME + ','
FROM
  Table
WHERE
  ID = @Id

SET @result = SUBSTRING(@result, 1, LEN(@result) - 1)

SELECT @result

Enjoy :D

like image 69
The Evil Greebo Avatar answered Jan 06 '23 21:01

The Evil Greebo