Generating Sorted List Becomes Easy with SQL Server 2017’s New String Function: String_AGG


  

 create table #temp
(id int,Name1 varchar(30),
Name2 varchar(30),
Name3 varchar(30) 
)

insert into #temp
values(1,'Ashley','Zebo','Terry'),
(2,'John','Adam','Bert') 

;with mycte as 
(select * from #temp
cross apply(Values(Name1),(Name2),(Name3)) d(Names) 
)

SELECT id, String_AGG(Names,',') WITHIN GROUP ( ORDER BY Names)  AS Result
FROM mycte
Group by id

drop table #temp 

 

https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s