Thursday, February 6, 2020

SQL Select into Comma-Delim String

Handy shortcut used to concatenate all areas into comma-delim string:

select
   distinct 
    stuff((
        select ',' + u.AREA_ID
        from myTableu
        where u.USER_ID = 'C1663'
        order by u.AREA_ID
        for xml path('')
    ),1,1,'') as userlist
from myTable
group by AREA_ID

No comments: