Skip to content

Commit 409ef2b

Browse files
authored
Merge pull request #19989 from WilliamDAssafMSFT/20210813-josteen-string-split-ordinal-docs
20210813 josteen string split ordinal docs
2 parents d99a6cc + 066de1a commit 409ef2b

1 file changed

Lines changed: 93 additions & 25 deletions

File tree

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

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
2-
title: "STRING_SPLIT (Transact-SQL) | Microsoft Docs"
2+
title: "STRING_SPLIT (Transact-SQL)"
33
description: "Transact-SQL reference for the STRING_SPLIT function. This table-valued function splits a string into substrings based on a character delimiter."
4-
ms.date: "11/28/2018"
4+
ms.date: "11/04/2021"
55
ms.prod: sql
66
ms.prod_service: "database-engine, sql-database"
7-
ms.reviewer: "jrasnick"
87
ms.technology: t-sql
98
ms.topic: reference
109
f1_keywords:
@@ -14,9 +13,9 @@ dev_langs:
1413
- "TSQL"
1514
helpviewer_keywords:
1615
- "STRING_SPLIT function"
17-
ms.assetid: 3273dbf3-0b4f-41e1-b97e-b4f67ad370b9
18-
author: julieMSFT
19-
ms.author: jrasnick
16+
author: WilliamDAssafMSFT
17+
ms.author: wiassaf
18+
ms.reviewer:
2019
monikerRange: "= azuresqldb-current||=azure-sqldw-latest||>= sql-server-2016 || >= sql-server-linux-2017"
2120
---
2221
# STRING_SPLIT (Transact-SQL)
@@ -32,39 +31,54 @@ STRING_SPLIT requires the compatibility level to be at least 130. When the level
3231
To change the compatibility level of a database, refer to [View or Change the Compatibility Level of a Database](../../relational-databases/databases/view-or-change-the-compatibility-level-of-a-database.md).
3332

3433
> [!NOTE]
35-
> Compatibility configuration is not needed for STRING_SPLIT in Azure Synapse Analytics.
34+
> Compatibility configuration is not needed for `STRING_SPLIT` in Azure Synapse Analytics.
3635
3736
![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
3837

3938
## Syntax
4039

4140
```syntaxsql
42-
STRING_SPLIT ( string , separator )
41+
STRING_SPLIT ( string , separator [ , enable_ordinal ] )
4342
```
4443

45-
[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)]
46-
4744
## Arguments
4845

49-
*string*
46+
#### *string*
5047
Is an [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any character type (for example, **nvarchar**, **varchar**, **nchar**, or **char**).
5148

52-
*separator*
49+
#### *separator*
5350
Is a single character [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any character type (for example, **nvarchar(1)**, **varchar(1)**, **nchar(1)**, or **char(1)**) that is used as separator for concatenated substrings.
54-
51+
52+
#### *enable_ordinal*
53+
An **int** or **bit** [expression](../../t-sql/language-elements/expressions-transact-sql.md) that serves as a flag to enable or disable the `ordinal` output column. A value of 1 enables the `ordinal` column. If *enable_ordinal* is omitted, `NULL`, or has a value of 0, the `ordinal` column is disabled.
54+
55+
> [!NOTE]
56+
> The *enable_ordinal* argument and `ordinal` output column are currently only supported in Azure SQL Database, Azure SQL Managed Instance, and Azure Synapse Analytics (serverless SQL pool only).
57+
5558
## Return Types
5659

57-
Returns a single-column table whose rows are the substrings. The name of the column is **value**. Returns **nvarchar** if any of the input arguments are either **nvarchar** or **nchar**. Otherwise returns **varchar**. The length of the return type is the same as the length of the string argument.
60+
If the `ordinal` output column is not enabled, STRING_SPLIT returns a single-column table whose rows are the substrings. The name of the column is `value`. It returns **nvarchar** if any of the input arguments are either **nvarchar** or **nchar**. Otherwise, it returns **varchar**. The length of the return type is the same as the length of the *string* argument.
61+
62+
If the *enable_ordinal* argument is passed a value of 1, a second column named `ordinal` is returned that consists of the 1-based index values of each substring's position in the input string. The return type is **bigint**.
63+
5864

5965
## Remarks
6066

61-
**STRING_SPLIT** inputs a string that has delimited substrings, and inputs one character to use as the delimiter or separator. STRING_SPLIT outputs a single-column table whose rows contain the substrings. The name of the output column is **value**.
67+
STRING_SPLIT inputs a string that has delimited substrings and inputs one character to use as the delimiter or separator. Optionally, the function supports a third argument with a value of 0 or 1 that disables or enables, respectively, the `ordinal` output column.
68+
69+
STRING_SPLIT outputs a single-column or double-column table, depending on the *enable_ordinal* argument.
6270

63-
The output rows might be in any order. The order is _not_ guaranteed to match the order of the substrings in the input string. You can override the final sort order by using an ORDER BY clause on the SELECT statement (`ORDER BY value`).
71+
- If *enable_ordinal* is `NULL`, omitted, or has a value of 0, STRING_SPLIT returns a single-column table whose rows contain the substrings. The name of the output column is `value`.
72+
73+
- If *enable_ordinal* has a value of 1, the function returns a two-column table, including the `ordinal` column that consists of the 1-based index values of the substrings in the original input string.
74+
75+
Note that the *enable_ordinal* argument must be a constant value, not a column or variable. It must also be either a **bit** or **int** data type with a value of 0 or 1. Otherwise, the function will raise an error.
76+
77+
The output rows might be in any order. The order is _not_ guaranteed to match the order of the substrings in the input string. You can override the final sort order by using an ORDER BY clause on the SELECT statement, for example, `ORDER BY value` or `ORDER BY ordinal`.
6478

6579
0x0000 (**char(0)**) is an undefined character in Windows collations and cannot be included in STRING_SPLIT.
6680

67-
Empty zero-length substrings are present when the input string contains two or more consecutive occurrences of the delimiter character. Empty substrings are treated the same as are plain substrings. You can filter out any rows that contain the empty substring by using the WHERE clause (`WHERE value <> ''`). If the input string is NULL, the STRING_SPLIT table-valued function returns an empty table.
81+
Empty zero-length substrings are present when the input string contains two or more consecutive occurrences of the delimiter character. Empty substrings are treated the same as are plain substrings. You can filter out any rows that contain the empty substring by using the WHERE clause, for example `WHERE value <> ''`. If the input string is `NULL`, the STRING_SPLIT table-valued function returns an empty table.
6882

6983
As an example, the following SELECT statement uses the space character as the separator:
7084

@@ -74,7 +88,7 @@ SELECT value FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');
7488

7589
In a practice run, the preceding SELECT returned following result table:
7690

77-
|value|
91+
|**value**|
7892
| :-- |
7993
|Lorem|
8094
|ipsum|
@@ -83,6 +97,23 @@ In a practice run, the preceding SELECT returned following result table:
8397
|amet.|
8498
| &nbsp; |
8599

100+
The following example enables the `ordinal` column by passing 1 for the optional third argument:
101+
102+
```sql
103+
SELECT value FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ', 1);
104+
```
105+
106+
This statement then returns the following result table:
107+
108+
|**value**|**ordinal**|
109+
| :-- | :-- |
110+
|Lorem|1|
111+
|ipsum|2|
112+
|dolor|3|
113+
|sit|4|
114+
|amet.|5|
115+
| &nbsp; ||
116+
86117
## Examples
87118

88119
### A. Split comma-separated value string
@@ -103,7 +134,7 @@ STRING_SPLIT will return empty string if there is nothing between separator. Con
103134

104135
Product table has a column with comma-separate list of tags shown in the following example:
105136

106-
|ProductId|Name|Tags|
137+
|**ProductId**|**Name**|**Tags**|
107138
|---------------|----------|----------|
108139
|1|Full-Finger Gloves|clothing,road,touring,bike|
109140
|2|LL Headset|bike|
@@ -119,7 +150,7 @@ FROM Product
119150

120151
[!INCLUDE[ssResult](../../includes/ssresult-md.md)]
121152

122-
|ProductId|Name|value|
153+
|**ProductId**|**Name**|**value**|
123154
|---------------|----------|-----------|
124155
|1|Full-Finger Gloves|clothing|
125156
|1|Full-Finger Gloves|road|
@@ -129,15 +160,15 @@ FROM Product
129160
|3|HL Mountain Frame|bike|
130161
|3|HL Mountain Frame|mountain|
131162

132-
>[!NOTE]
133-
> The order of the output may vary as the order is _not_ guaranteed to match the order of the substrings in the input string.
134-
163+
>[!NOTE]
164+
> The order of the output may vary as the order is _not_ guaranteed to match the order of the substrings in the input string.
165+
135166
### C. Aggregation by values
136167

137168
Users must create a report that shows the number of products per each tag, ordered by number of products, and to filter only the tags with more than two products.
138169

139170
```sql
140-
SELECT value as tag, COUNT(*) AS [Number of articles]
171+
SELECT value as tag, COUNT(*) AS [number_of_articles]
141172
FROM Product
142173
CROSS APPLY STRING_SPLIT(Tags, ',')
143174
GROUP BY value
@@ -186,7 +217,44 @@ FROM Product
186217
WHERE ',1,2,3,' LIKE '%,' + CAST(ProductId AS VARCHAR(20)) + ',%';
187218
```
188219

189-
## See Also
220+
### F. Find rows by ordinal values
221+
222+
The following statement finds all rows with an even index value:
223+
224+
```sql
225+
SELECT *
226+
FROM STRING_SPLIT('Austin,Texas,Seattle,Washington,Denver,Colorado', ',', 1)
227+
WHERE ordinal % 2 = 0;
228+
```
229+
230+
The above statement returns the following table:
231+
232+
|**value**|**ordinal**|
233+
|----------|--------|
234+
|Texas|2|
235+
|Washington|4|
236+
|Colorado|6|
237+
238+
## G. Order rows by ordinal values
239+
240+
The following statement returns the split substring values of the input string and their ordinal values, ordered by the `ordinal` column:
241+
242+
```sql
243+
SELECT * FROM STRING_SPLIT('E-D-C-B-A', '-', 1) ORDER BY ordinal DESC;
244+
```
245+
246+
The above statement returns the following table:
247+
248+
|**value**|**ordinal**|
249+
|-----|--------|
250+
|A|5|
251+
|B|4|
252+
|C|3|
253+
|D|2|
254+
|E|1|
255+
256+
257+
## Next Steps
190258

191259
- [LEFT &#40;Transact-SQL&#41;](../../t-sql/functions/left-transact-sql.md)
192260
- [LTRIM &#40;Transact-SQL&#41;](../../t-sql/functions/ltrim-transact-sql.md)

0 commit comments

Comments
 (0)