Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Created June 27, 2013 01:11
Show Gist options
  • Save ryanguill/5873218 to your computer and use it in GitHub Desktop.
Save ryanguill/5873218 to your computer and use it in GitHub Desktop.
for xml path example
use tempdb;
IF OBJECT_ID('tempdb..#customerPreferences') IS NOT NULL
DROP TABLE #customerPreferences
create table #customerPreferences ( username varchar(50), pref varchar(50) );
select * from #customerPreferences
insert into #customerPreferences ( username, pref )
select 'ryan', 'email'
union all
select 'ryan', 'phone'
union all
select 'ryan', 'fax'
union all
select 'shane', 'email'
select * from #customerPreferences
select distinct
username
, substring((SELECT
',' + b.pref
FROM #customerPreferences b
WHERE a.username = b.username
ORDER BY
b.pref ASC
FOR XML PATH( '' )
), 2, 1000 ) as prefs
from #customerPreferences a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment