Creating comma-delimited strings

Need to create a comma-delimited string from a list of values?  This little trick never fails to amaze SQL programmers the first time they see it. 

Need to create a comma-delimited string from a list of values?  This little trick never fails to amaze SQL programmers the first time they see it. 

declare @DelimitedString varchar(500)

Select  @DelimitedString = isnull(@DelimitedString + ', ', '') + FieldValue
From    YourTable
Where   YourCriteria

Select @DelimitedString

It seems like it should not work, but it does.  Note that it can only be used to process one string at a time.  It works well as a user-defined function.