[!div class=“nextstepaction”] Please help improve SQL Server docs!
Splits the character expression using specified separator.
[!NOTE]
The STRING_SPLIT function is available only under compatibility level 130 and above. If your database compatibility level is lower than 130, SQL Server will not be able to find and execute STRING_SPLIT function. To change the compatibility level of a database, refer to View or Change the Compatibility Level of a Database. Note that compatibility level 120 might be default even in new SQL Server Azure SQL Database
Transact-SQL Syntax Conventions
STRING_SPLIT ( string , separator )
string
Is an expression of any character type (for example, nvarchar, varchar, nchar, or char).
separator
Is a single character expression of any character type (for example, nvarchar(1), varchar(1), nchar(1), or char(1)) that is used as separator for concatenated strings.
Returns a single-column table with fragments. 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.
STRING_SPLIT takes a string that should be divided and the separator that will be used to divide string. It returns a single-column table with substrings. For example, the following statement SELECT value FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');
using the space character as the separator, returns following result table:
value |
---|
Lorem |
ipsum |
dolor |
sit |
amet. |
If the input string is NULL, the STRING_SPLIT table-valued function returns an empty table.
STRING_SPLIT requires at least compatibility mode 130.
Parse a comma separated list of values and return all non-empty tokens:
DECLARE @tags NVARCHAR(400) = 'clothing,road,,touring,bike'
SELECT value
FROM STRING_SPLIT(@tags, ',')
WHERE RTRIM(value) <> '';
STRING_SPLIT will return empty string if there is nothing between separator. Condition RTRIM(value) <> ’’ will remove empty tokens.
Product table has a column with comma-separate list of tags shown in the following example:
ProductId | Name | Tags |
---|---|---|
1 | Full-Finger Gloves | clothing,road,touring,bike |
2 | LL Headset | bike |
3 | HL Mountain Frame | bike,mountain |
Following query transforms each list of tags and joins them with the original row:
Here is the result set.
ProductId | Name | value |
---|---|---|
1 | Full-Finger Gloves | clothing |
1 | Full-Finger Gloves | road |
1 | Full-Finger Gloves | touring |
1 | Full-Finger Gloves | bike |
2 | LL Headset | bike |
3 | HL Mountain Frame | bike |
3 | HL Mountain Frame | mountain |
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.
SELECT value as tag, COUNT(*) AS [Number of articles]
FROM Product
CROSS APPLY STRING_SPLIT(Tags, ',')
GROUP BY value
HAVING COUNT(*) > 2
ORDER BY COUNT(*) DESC;
Developers must create queries that find articles by keywords. They can use following queries:
To find products with a single tag (clothing):
SELECT ProductId, Name, Tags
FROM Product
WHERE 'clothing' IN (SELECT value FROM STRING_SPLIT(Tags, ','));
Find products with two specified tags (clothing and road):
SELECT ProductId, Name, Tags
FROM Product
WHERE EXISTS (SELECT *
FROM STRING_SPLIT(Tags, ',')
WHERE value IN ('clothing', 'road');
Developers must create a query that finds articles by a list of IDs. They can use following query:
This is replacement for common anti-pattern such as creating a dynamic SQL string in application layer or Transact\-SQL or by using LIKE operator:
SELECT ProductId, Name, Tags
FROM Product
WHERE ',1,2,3,' LIKE '%,' + CAST(ProductId AS VARCHAR(20)) + ',%';
LEFT (Transact-SQL)
LTRIM (Transact-SQL)
RIGHT (Transact-SQL)
RTRIM (Transact-SQL)
SUBSTRING (Transact-SQL)
TRIM (Transact-SQL)
String Functions (Transact-SQL)