Skip to content

Commit 57458f2

Browse files
Merge pull request #34712 from rwestMSFT/rw-0716-agg-fix-124333
Fix second STRING_AGG example and refresh article (124333)
2 parents 7163fca + d684f88 commit 57458f2

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

docs/t-sql/functions/string-agg-transact-sql.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
title: "STRING_AGG (Transact-SQL)"
3-
description: STRING_AGG concatenates the values of string expressions and places separator values between them."
3+
description: STRING_AGG concatenates the values of string expressions and places separator values between them.
44
author: MikeRayMSFT
55
ms.author: mikeray
66
ms.reviewer: randolphwest
7-
ms.date: 01/16/2025
7+
ms.date: 07/16/2025
88
ms.service: sql
99
ms.subservice: t-sql
1010
ms.topic: reference
@@ -38,11 +38,11 @@ STRING_AGG ( expression , separator ) [ <order_clause> ]
3838

3939
#### *expression*
4040

41-
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type. Expressions are converted to **nvarchar** or **varchar** types during concatenation. Non-string types are converted to **nvarchar** type.
41+
An [expression](../language-elements/expressions-transact-sql.md) of any type. Expressions are converted to **nvarchar** or **varchar** types during concatenation. Non-string types are converted to **nvarchar** type.
4242

4343
#### *separator*
4444

45-
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of **nvarchar** or **varchar** type that is used as separator for concatenated strings. It can be literal or variable.
45+
An [expression](../language-elements/expressions-transact-sql.md) of **nvarchar** or **varchar** type that is used as separator for concatenated strings. It can be literal or variable.
4646

4747
#### <order_clause>
4848

@@ -54,7 +54,7 @@ WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )
5454

5555
- `<order_by_expression_list>`
5656

57-
A list of non-constant [expressions](../../t-sql/language-elements/expressions-transact-sql.md) that can be used for sorting results. Only one `<order_by_expression_list>` is allowed per query. The default sort order is ascending.
57+
A list of non-constant [expressions](../language-elements/expressions-transact-sql.md) that can be used for sorting results. Only one `<order_by_expression_list>` is allowed per query. The default sort order is ascending.
5858

5959
## Return types
6060

@@ -70,11 +70,11 @@ Return type depends on first argument (expression). If input argument is string
7070

7171
## Remarks
7272

73-
`STRING_AGG` is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions. For more information about data type conversions, see [CAST and CONVERT](../../t-sql/functions/cast-and-convert-transact-sql.md).
73+
`STRING_AGG` is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions. For more information about data type conversions, see [CAST and CONVERT](cast-and-convert-transact-sql.md).
7474

7575
If the input expression is type **varchar**, the separator can't be type **nvarchar**.
7676

77-
Null values are ignored and the corresponding separator isn't added. To return a place holder for null values, use the `ISNULL` function as demonstrated in [example B](#b-generate-list-of-names-separated-with-comma-without-null-values).
77+
Null values are ignored and the corresponding separator isn't added. To return a place holder for null values, use the `ISNULL` function as demonstrated in [example B](#b-generate-list-of-middle-names-separated-with-comma-without-null-values).
7878

7979
`STRING_AGG` is available in any compatibility level.
8080

@@ -98,7 +98,7 @@ FROM Person.Person;
9898
GO
9999
```
100100

101-
[!INCLUDE [ssResult_md](../../includes/ssresult-md.md)]
101+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
102102

103103
```output
104104
csv
@@ -115,18 +115,17 @@ Hazem
115115
`NULL` values found in `name` cells aren't returned in the result.
116116

117117
> [!NOTE]
118-
> If using the [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] Query Editor, the **Results to Grid** option can't implement the carriage return. Switch to **Results to Text** to see the result set properly.
119-
> Results to Text are truncated to 256 characters by default. To increase this limit, change the **Maximum number of characters displayed in each column** option.
118+
> If you use the [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] Query Editor, the **Results to Grid** option can't implement the carriage return. Switch to **Results to Text** to see the result set properly. Results to Text are truncated to 256 characters by default. To increase this limit, change the **Maximum number of characters displayed in each column** option.
120119
121-
### B. Generate list of names separated with comma without `NULL` values
120+
### B. Generate list of middle names separated with comma without NULL values
122121

123-
The following example replaces null values with 'N/A' and returns the names separated by commas in a single result cell.
122+
The following example replaces `NULL` values with `N/A` and returns the names separated by commas in a single result cell.
124123

125124
```sql
126125
USE AdventureWorks2022;
127126
GO
128127

129-
SELECT STRING_AGG(CONVERT (NVARCHAR (MAX), ISNULL(FirstName, 'N/A')), ',') AS csv
128+
SELECT STRING_AGG(CONVERT (NVARCHAR (MAX), ISNULL(MiddleName, 'N/A')), ',') AS csv
130129
FROM Person.Person;
131130
GO
132131
```
@@ -136,7 +135,7 @@ Here's a trimmed result set.
136135
```output
137136
csv
138137
-----
139-
Syed,Catherine,Kim,Kim,Kim,Hazem,Sam,Humberto,Gustavo,Pilar,Pilar, ...
138+
E,R.,N/A,N/A,B,E,N/A,N/A,N/A,N/A,G,B,N/A,C,J,L,P,N/A,M,N/A,N/A,N/A,L,J., ...
140139
```
141140

142141
### C. Generate comma-separated values
@@ -163,7 +162,7 @@ Rob Walters (Dec 29 2001 12:00AM)
163162
```
164163

165164
> [!NOTE]
166-
> If using the Management Studio Query Editor, the **Results to Grid** option can't implement the carriage return. Switch to **Results to Text** to see the result set properly.
165+
> If you use the [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] Query Editor, the **Results to Grid** option can't implement the carriage return. Switch to **Results to Text** to see the result set properly. Results to Text are truncated to 256 characters by default. To increase this limit, change the **Maximum number of characters displayed in each column** option.
167166
168167
### D. Return news articles with related tags
169168

@@ -180,7 +179,7 @@ GROUP BY a.articleId, title;
180179
GO
181180
```
182181

183-
[!INCLUDE [ssResult_md](../../includes/ssresult-md.md)]
182+
[!INCLUDE [ssresult-md](../../includes/ssresult-md.md)]
184183

185184
| articleId | title | tags |
186185
| --- | --- | --- |
@@ -210,10 +209,7 @@ GROUP BY City;
210209
GO
211210
```
212211

213-
[!INCLUDE [ssResult_md](../../includes/ssresult-md.md)]
214-
215-
> [!NOTE]
216-
> Results are shown trimmed.
212+
Here's the trimmed result set.
217213

218214
| City | emails |
219215
| --- | --- |
@@ -249,10 +245,7 @@ GROUP BY City;
249245
GO
250246
```
251247

252-
[!INCLUDE [ssResult_md](../../includes/ssresult-md.md)]
253-
254-
> [!NOTE]
255-
> Results are shown trimmed.
248+
Here's the trimmed result set.
256249

257250
| City | Emails |
258251
| --- | --- |

0 commit comments

Comments
 (0)