STRING_SPLIT SQL Server Syntax & Examples

How-to basics · Updated November 23, 2026

The STRING_SPLIT function in SQL Server splits a string into rows based on a separator. Its syntax is STRING_SPLIT ( string , separator ), which returns a single-column table named value containing the substrings. For example, SELECT value FROM STRING_SPLIT('apple,banana,cherry', ','); returns three rows: ‘apple’, ‘banana’, and ‘cherry’. This function is available starting with SQL Server 2016 and in Azure SQL Database.

Background and Why It Exists

Before SQL Server 2016, splitting a delimited string was a notorious pain point. Developers used complex recursive CTEs, XML-based splitting, or custom user-defined functions. Those methods were slow, hard to read, and often broke with special characters. Microsoft introduced STRING_SPLIT to provide a built-in, set-based way to break strings into rows, which is much faster and cleaner. It is part of the “intelligent query processing” family that aims to simplify common tasks. The function is deterministic and does not require a numbers table or loop.

How STRING_SPLIT Works

The function takes two arguments: the input string and a single-character separator. It returns a table with one column named value. The output rows appear in the same order as the substrings in the input, but the documentation does not guarantee that order unless you use ORDER BY (which you cannot do directly on the output column because it is not guaranteed). In practice, the order matches the input order for most collations, but you should not rely on it. The separator can be any single character, including a space, comma, semicolon, or a non-printable character. If the input string is NULL, the function returns an empty table. If the separator is NULL, it also returns an empty table. If the string is empty, it returns zero rows.

Example with a space separator:

SELECT value FROM STRING_SPLIT('Hello world from SQL', ' ');
-- Returns: Hello, world, from, SQL

Note that STRING_SPLIT does not remove empty strings. If your input has consecutive delimiters, you get empty rows. For instance, STRING_SPLIT('a,,b', ',') returns ‘a’, ”, ‘b’. If you need to filter out empties, use a WHERE clause: WHERE value <> ''.

Practical Examples and Use Cases

Here are common scenarios where STRING_SPLIT shines:

  • Parsing comma-separated IDs for a query: Instead of dynamic SQL, you can split a parameter like '1,2,3' and join it to your table.
  • Normalizing denormalized data: If a column contains tags like ‘red;green;blue’, you can split and insert into a child table.
  • Searching for multiple values: Use WHERE column IN (SELECT value FROM STRING_SPLIT(@list, ',')).
  • Counting items: Use SELECT COUNT(*) FROM STRING_SPLIT('a,b,c', ',') to get 3.

Example: Join to a table of products using a comma-separated list of product IDs:

DECLARE @ids NVARCHAR(100) = '1,3,5';
SELECT p.ProductName
FROM Products p
INNER JOIN STRING_SPLIT(@ids, ',') s ON p.ProductID = CAST(s.value AS INT);

Notice that you need to cast value to the appropriate data type (INT, DATE, etc.) because value is always nvarchar.

Choosing the Right Separator and Handling Edge Cases

Because the separator is a single character, you cannot use multi-character delimiters like ‘||’. If you need that, you have to use a different approach (e.g., REPLACE first). Also, be careful with collation: the separator comparison uses the collation of the input string. If you use a case-sensitive collation, ‘A’ and ‘a’ are different; if case-insensitive, they are the same. Usually that does not matter for separators like commas, but it can for letters. If your data contains the separator as part of a value (e.g., an address with commas), you cannot escape it—STRING_SPLIT has no escape mechanism. In such cases, consider using a different delimiter or a JSON/XML approach. For example, if you have 'John, Doe, 123 Main St' and you split by comma, you get three parts, not two. Plan accordingly.

Performance and Limitations

STRING_SPLIT is generally fast, but there are limitations:

  • It does not return an ordinal (position) column. If you need the position of each item, you must use a different method, such as a JSON-based split or a recursive CTE. In SQL Server 2022, you can use OPENJSON with a trick to get ordinal, but that is more complex.
  • The maximum input string size is limited by the nvarchar(max) or varchar(max) data type, so up to 2 GB. For very large strings, performance may degrade.
  • It is not available in SQL Server 2014 or earlier, nor in Azure SQL Data Warehouse (now Synapse) for some versions. Check your version.
  • You cannot use STRING_SPLIT in a computed column or an indexed view directly because it is a table-valued function.

For large strings, the function uses a memory-optimized approach, but it still materializes the full result set. If you are splitting millions of rows, consider using a numbers table or a CLR function for better performance, but for most typical uses, STRING_SPLIT is sufficient.

Alternatives and Comparisons

If STRING_SPLIT does not meet your needs, here are alternatives:

  • XML method: SELECT value FROM (SELECT CAST('<x>' + REPLACE(@s, ',', '</x><x>') + '</x>' AS XML) AS x) t CROSS APPLY x.nodes('/x') AS n(value). This works in older versions but is slower and has issues with special XML characters.
  • JSON: In SQL Server 2016+, you can use OPENJSON to split with an array: SELECT value FROM OPENJSON('["a","b"]') but you must build the JSON string first. It also gives you a key column for ordinal.
  • Recursive CTE: Works in all versions but can be slow for long strings.
  • Custom CLR function: Fast but requires assembly deployment and is not recommended unless you have heavy usage.

For most cases, STRING_SPLIT is the simplest and fastest built-in option. If you need ordinal positions, use OPENJSON with a generated array, as shown in this example:

DECLARE @s NVARCHAR(100) = 'apple,banana,cherry';
SELECT [key] + 1 AS Position, value
FROM OPENJSON('["' + REPLACE(@s, ',', '","') + '"]');

This returns Position 1,2,3 with the corresponding values. Note that this technique fails if your string contains double quotes or backslashes, so it is not a universal replacement.

Frequently Asked Questions

How do I get the position of each element in STRING_SPLIT?

As of SQL Server 2022, there is no built-in ordinal column. You can use ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) but the order is not guaranteed. The safest way is to use OPENJSON as shown above, or use a numbers table approach. In SQL Server 2022, you can also use STRING_SPLIT with the enable_ordinal parameter? Actually, that parameter does not exist. The documentation for SQL Server 2022 still says no ordinal. So stick with OPENJSON or a custom function.

Can I use STRING_SPLIT with a multi-character delimiter?

No, the separator must be a single character. If you have a multi-character delimiter, you can first replace it with a single character that does not appear in your data, then split. For example, to split by ‘||’, use REPLACE(@s, '||', '|') and then split by ‘|’. This works if ‘|’ is not in your data. Otherwise, you need a different method.

Why does STRING_SPLIT return an empty row for consecutive delimiters?

Because the function treats each separator as a boundary, and an empty string between two separators is a valid substring. If you do not want empty rows, filter them with WHERE value <> ''.

Is STRING_SPLIT available in Azure SQL Database?

Yes, it is available in Azure SQL Database and Azure SQL Managed Instance. It is also in SQL Server 2016 and later. It is not available in Azure Synapse Analytics (dedicated SQL pool) as of the latest updates; you need to use a workaround there.

What is the data type of the output column?

The output column is named value and its data type is nvarchar(max) if the input is nvarchar, or varchar(max) if the input is varchar. You often need to cast it to the desired type, like CAST(value AS INT).

Can I use STRING_SPLIT in a view or a function?

Yes, you can use it in a view or a table-valued function, but not in a computed column. For example, you can create a view that splits a column on the fly, but note that the view will not be indexed because of the function call.

Conclusion

STRING_SPLIT is a powerful, built-in function that simplifies string splitting in SQL Server. It is fast, easy to use, and handles most common scenarios. Just be aware of its limitations: single-character separator, no ordinal, and no escape mechanism. For advanced needs, consider OPENJSON or custom code. Always test with your actual data and collation to ensure the output matches expectations.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.