Sometimes you want to concatinate data from multiple records into one field. For example a list of states could be
1 | Alabama |
or it could be
1 | Alabama,Alaska,Arizona... |
This second output is an example of concatenation
Suppose you wanted a list of employees who are 42 years old. You could have a sql statement similar to the following:
1 | select ','+empUserName |
you might receive a list similar to the following
1 | AgxxxxxAm |
But suppose you wanted the list in one field, comma seperated. Then you could use a combination of the for xml path(‘’) operator (to perform concatination services) and the Stuff function (to trim off leading comma).
1 | select stuff( |
This will give you an output similar to the following
1 | AgxxxxxAm,AnxxxL,ZarxxxraR,ZiexxxnE |