For a clean and efficient solution you can create an user defined aggregate function, there is even an example that does just what you need.
You can then use it like any other aggregate function (with a standard query plan):
More Related Contents:
- How to concatenate text from multiple rows into a single text string in SQL Server
- How to use GROUP BY to concatenate strings in SQL Server?
- How to create a SQL Server function to “join” multiple rows from a subquery into a single delimited field? [duplicate]
- SQL NVARCHAR and VARCHAR Limits
- GROUP BY to combine/concat a column [duplicate]
- SQL GROUP BY CASE statement with aggregate function
- sql group by only rows which are in sequence
- How to return a incremental group number per group in SQL
- SQL query to group by day
- Conversion failed when converting the varchar value ‘simple, ‘ to data type int
- How to group by a Calculated Field
- How to do a case sensitive GROUP BY?
- NOT IN vs NOT EXISTS
- Only inserting a row if it’s not already there
- How to concatenate strings of a string field in a PostgreSQL ‘group by’ query?
- Most efficient way in SQL Server to get date from date+time?
- When no ‘Order by’ is specified, what order does a query choose for your record set?
- How do I create a step in my SQL Server Agent Job which will run my SSIS package?
- SQL Call Stored Procedure for each Row without using a cursor
- How to select the last record of a table in SQL?
- How do I group on continuous ranges
- join comma delimited data column
- Is it possible to perform multiple updates with a single UPDATE SQL statement?
- Get previous and next row from rows selected with (WHERE) conditions
- How to list files inside a folder with SQL Server
- Can SQL Server Express LocalDB be connected to remotely?
- Is there a severe performance hit for using Foreign Keys in SQL Server?
- How can I find Unicode/non-ASCII characters in an NTEXT field in a SQL Server 2005 table?
- Date serial in SQL?
- Parameterise table name in .NET/SQL?