---
title: "Help with Writing a Query to Combine Multiple Rows into One in SQL Server"  
description: "Help with Writing a Query to Combine Multiple Rows into One in SQL Server"  
author: "ICSM Computer"  
published: 2024-07-16  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160929/help-with-writing-a-query-to-combine-multiple-rows-into-one-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# Help with Writing a Query to Combine Multiple Rows into One in SQL Server

I have a [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) `Orders` and I need to [combine multiple](https://answers.mindstick.com/qa/95157/how-do-i-combine-multiple-file-types-into-a-single-pdf) rows for the same `CustomerID` into a [single row](https://www.mindstick.com/forum/12960/how-do-i-get-a-single-row-from-a-linq-expression-in-c-sharp), concatenating the `OrderID`s. Can someone assist me?

## Replies

### Reply by Ravi Vishwakarma

Combining [multiple rows](https://www.mindstick.com/forum/160908/how-to-concatenate-text-from-multiple-rows-into-a-single-text-string-in-sql-server) into one in SQL Server can be achieved using the `STRING_AGG` function (available from SQL Server 2017 onwards) or using the `FOR XML PATH` method for earlier versions. Here are examples of both methods.

#### Using `STRING_AGG` (SQL Server 2017+)

Suppose you have a table `your_table` with a column `your_column`, and you want to concatenate all the values in `your_column` into a single string:

```plaintext
--Syntax
SELECT STRING_AGG(your_column, ', ') AS concatenated_string
FROM your_table;

--Example
SELECT STRING_AGG(ID, ', ') AS concatenated_string
FROM Article;
```

#### Using `FOR XML PATH` (SQL Server 2016 and earlier)

If you're using a version of SQL Server before 2017, you can achieve the same result using the `FOR XML PATH` method:

```plaintext
--Syntax
SELECT STUFF(
    (SELECT ', ' + your_column
     FROM your_table
     FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)'),
    1, 2, ''
) AS concatenated_string;

--Example
SELECT STUFF(
    (SELECT ', ' + CAST(ID AS VARCHAR)
     FROM Article
     FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)'),
    1, 2, ''
) AS concatenated_string;
```

### Explanation

`STRING_AGG` **method**:

- `STRING_AGG(your_column, ', ')`: Concatenates the values of `your_column` with a comma and a space as the separator.

`FOR XML PATH` **method**:

- `(SELECT ', ' + your_column FROM your_table FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')`: Creates an XML string of the concatenated values, each prefixed with a comma and a space.
- `STUFF(..., 1, 2, '')`: Removes the first comma and space from the concatenated string.

**Read more**\
[**Explain the SQL CURSOR with an example.**](https://www.mindstick.com/blog/304490/explain-the-sql-cursor-with-example)

[**How can I optimize SQL Server queries to improve performance?**](https://www.mindstick.com/blog/304489/how-can-i-optimize-sql-server-queries-to-improve-performance)

[**How do I handle NULL values in SQL Server queries and avoid**](https://www.mindstick.com/blog/304492/how-do-i-handle-null-values-in-sql-server-queries-and-avoid-common-pitfalls)

[**SQL Query to Get Yearly Aggregated Data in SQL Server**](https://www.mindstick.com/forum/160930/sql-query-to-get-yearly-aggregated-data-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160929/help-with-writing-a-query-to-combine-multiple-rows-into-one-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
