Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql column values comma separated

SELECT STRING_AGG(columnName,',') from tableName
Comment

sql server: select column values as comma separated string

 DECLARE @SQL AS VARCHAR(8000)
SELECT @SQL = ISNULL(@SQL+',','') + ColumnName FROM TableName
SELECT @SQL
Comment

get comma separated values in sql server

STRING_AGG: Applies to SQL Server 2017 (14.x) and later 

DECLARE @Table2 TABLE(ID INT, Value INT);
INSERT INTO @Table1 VALUES (1,100),(1,200),(1,300),(1,400);

SELECT ID , STRING_AGG([Value], ', ') AS List_Output
FROM @Table2
GROUP BY ID;

-- OR 

DECLARE @Table1 TABLE(ID INT, Value INT)
INSERT INTO @Table1 VALUES (1,100),(1,200),(1,300),(1,400)

SELECT  ID
       ,STUFF((SELECT ', ' + CAST(Value AS VARCHAR(10)) [text()]
         FROM @Table1 
         WHERE ID = t.ID
         FOR XML PATH(''), TYPE)
        .value('.','NVARCHAR(MAX)'),1,1,' ') List_Output
FROM @Table1 t
GROUP BY ID
Comment

PREVIOUS NEXT
Code Example
Sql :: oracle alter table add not null constraint 
Sql :: postgresql difference between two dates in days 
Sql :: postgresql add alter permissions to role 
Sql :: add created and updatedAt fields in mysql 
Sql :: add days in oracle sql 
Sql :: mysql repair a table 
Sql :: v$session table or view does not exist 
Sql :: oracle list service names 
Sql :: replace null with 0 in sql 
Sql :: sql remove non numeric characters 
Sql :: create sqlite database in laravel 
Sql :: how to add not null constraint in sql 
Sql :: postgresql where datetrunc month and year equal 
Sql :: postgres alter table add column with default value 
Sql :: MySql get primary keys of table 
Sql :: mysql update set sum 
Sql :: how to define a composite primary key in sql 
Sql :: pl sql asynchronous procedure calls 
Sql :: what is mysql_pconnect 
Sql :: sql string starts with 
Sql :: mysql previous year 
Sql :: docker export psql sql 
Sql :: oracle db get table sizes 
Sql :: create in sql 
Sql :: dynamic sql invalid table name 
Sql :: enable service broker in sql server 
Sql :: sql rank 
Sql :: sql not null 
Sql :: sql truncate statement 
Sql :: mysql query first character 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =