SQL Server Split String: A Comprehensive Guide for Devs

Hi Dev, are you struggling to split strings in SQL Server? If yes, you’re not alone. String manipulation is a common problem for developers, but SQL Server has a built-in function that can help with this. In this article, we’ll discuss everything you need to know about SQL Server split string function, covering the basics, advanced techniques, and frequently asked questions. So, let’s get started!

Table of Contents

  1. Basics of SQL Server Split String Function
  2. Choosing the Right Delimiter
  3. Performance Considerations
  4. Advanced Techniques for Splitting Strings
  5. Handling Nulls and Empty Strings
  6. Limiting the Number of Substrings
  7. Using Regular Expressions
  8. Using XML to Split Strings
  9. Frequently Asked Questions

Basics of SQL Server Split String Function

The SQL Server split string function is called STRING_SPLIT, and as the name suggests, it allows you to split a string into substrings based on a delimiter. Here’s the basic syntax:

Parameter
Description
string
The string to be split
separator
The delimiter that separates the substrings

Here’s an example:

SELECT * FROM STRING_SPLIT('apple,banana,orange', ',')

This will split the string 'apple,banana,orange' into three substrings, using the comma as the delimiter. The output will be:

value-----applebananaorange

As you can see, the result is a table with a single column, value, that contains the substrings.

How to Use the Results

Once you have split the string into substrings, you can use the value column in various ways. For example, you can join it with another table or use it in a WHERE clause. Here’s an example:

SELECT * FROM Orders WHERE CustomerName IN (SELECT value FROM STRING_SPLIT('John,Jane', ','))

This will select all orders where the customer name is either John or Jane.

Limitations of STRING_SPLIT

While STRING_SPLIT is a handy function, it has a few limitations that you should be aware of:

  • It only works on SQL Server 2016 or later versions.
  • It doesn’t support multi-character delimiters.
  • It doesn’t support escaping delimiters.
  • It doesn’t support custom sorting or filtering options.

However, these limitations can be overcome with various techniques that we’ll discuss later in this article.

Choosing the Right Delimiter

Choosing the right delimiter is crucial for successful string splitting. Here are some common delimiters and how to use them:

Comma Delimiter

The comma is the most common delimiter, and it’s a good choice if your string doesn’t contain commas. Here’s an example:

SELECT * FROM STRING_SPLIT('apple,banana,orange', ',')

The output will be:

value-----applebananaorange

Pipe Delimiter

The pipe (|) is another common delimiter that works well if your string contains commas. Here’s an example:

SELECT * FROM STRING_SPLIT('apple|banana|orange', '|')

The output will be:

value-----applebananaorange

Semicolon Delimiter

The semicolon (;) is a good choice if your string contains commas and pipes, as they are less common in text. Here’s an example:

SELECT * FROM STRING_SPLIT('apple;banana;orange', ';')

The output will be:

value-----applebananaorange

Space Delimiter

The space is a good choice if your string contains words separated by spaces. Here’s an example:

SELECT * FROM STRING_SPLIT('apple banana orange', ' ')

The output will be:

value-----applebananaorange

Custom Delimiter

If your string contains a custom delimiter, you can still use STRING_SPLIT by replacing the delimiter with a common delimiter, such as a comma or a pipe, and then splitting the string using that delimiter. Here’s an example:

SELECT * FROM STRING_SPLIT(REPLACE('apple#banana#orange', '#', ','), ',')

This first replaces the hash symbol with a comma, and then splits the string using a comma delimiter. The output will be:

value-----applebananaorange

Performance Considerations

When splitting large strings or processing many strings, performance can be a concern. Here are some tips to improve performance:

Use a Table-Valued Function

Instead of repeating the STRING_SPLIT function call for each string, you can create a table-valued function that accepts a string and returns the resulting table. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEASRETURNSELECT * FROM STRING_SPLIT(@string, @delimiter)GOSELECT * FROM SplitString('apple,banana,orange', ',')

This will create a function SplitString that can be called with a string and a delimiter. The output will be same as previous examples.

Limit the Input Size

If you know the maximum size of the input string, you can limit the size of the varchar variable that holds the string, which can improve performance. Here’s an example:

DECLARE @string VARCHAR(100) = 'apple,banana,orange'SELECT * FROM STRING_SPLIT(@string, ',')

This limits the size of the @string variable to 100 characters, which is sufficient for the example string, but saves memory and improves performance.

READ ALSO  How to Host Files on Apache Web Server

Use an Inline Table-Valued Function

An inline table-valued function is a single expression that returns a table. It can be faster than a multi-statement table-valued function, especially for small input strings. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEAS RETURN(SELECT value FROM STRING_SPLIT(@string, @delimiter))GOSELECT * FROM SplitString('apple,banana,orange', ',')

This will create an inline table-valued function SplitString that returns only the value column, which is what we need in most cases. The output will be same as previous examples.

Advanced Techniques for Splitting Strings

If the basic STRING_SPLIT function doesn’t meet your requirements, you can use various techniques to achieve the desired result. Here are some advanced techniques:

Splitting Strings with Multi-Character Delimiters

If your string contains a multi-character delimiter, such as '--', '|*|', or ';;', you can use a combination of CHARINDEX and SUBSTRING functions to split the string. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(MAX))RETURNS TABLEAS RETURN(SELECT SUBSTRING(@string, start, CHARINDEX(@delimiter, @string + @delimiter, start) - start) AS valueFROM (SELECT (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) - 1 AS startFROM (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) x(n)CROSS JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) y(n)WHERE LEN(@string) > start) numbersWHERE SUBSTRING(@delimiter + @string, start, LEN(@delimiter)) = @delimiter)GOSELECT * FROM SplitString('apple--banana--orange', '--')

This will create a function SplitString that can split strings with multi-character delimiters. The output will be:

value-----applebananaorange

Splitting Strings with Escaped Delimiters

If your string contains escaped delimiters, such as '\,', '\|', or '\t', you can use a combination of REPLACE and STRING_SPLIT functions to split the string. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEAS RETURN(SELECT REPLACE(value, '\\' + @delimiter, @delimiter) AS valueFROM STRING_SPLIT(REPLACE(@string, '\\' + @delimiter, '@@'), @delimiter)WHERE value <> '@@')GOSELECT * FROM SplitString('apple,\,banana,orange', ',')

This will create a function SplitString that can split strings with escaped delimiters. The output will be:

value-----apple,bananaorange

Splitting Strings with Custom Sorting and Filtering Options

If you need custom sorting or filtering options for the substrings, you can use a combination of STRING_SPLIT and CROSS APPLY functions. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEAS RETURN(SELECT valueFROM STRING_SPLIT(@string, @delimiter)CROSS APPLY (SELECT IIF(value LIKE 'A%', 1, 2) AS priority) pORDER BY p.priority, valueWHERE LEN(value) > 0)GOSELECT * FROM SplitString('apple,banana,orange,artichoke', ',')

This will create a function SplitString that sorts the substrings that start with 'A' first, and the remaining substrings second. The output will be:

value-----appleartichokebananaorange

Handling Nulls and Empty Strings

If your input string is NULL or empty, or if the delimiter is not found in the string, the STRING_SPLIT function returns an empty table, which can cause errors or unexpected output. Here’s how to handle these cases:

Handling NULL or Empty Strings

If your input string can be NULL or empty, you can use a combination of ISNULL and LEN functions to return an empty table when the input is NULL or empty. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEAS RETURN(SELECT valueFROM STRING_SPLIT(ISNULL(NULLIF(@string, ''), 'N/A'), @delimiter)WHERE LEN(value) > 0)GOSELECT * FROM SplitString(NULL, ',')

This will create a function SplitString that returns an empty table when the input is NULL or empty. The output will be an empty table.

Handling Missing Delimiters

If the delimiter is not found in the string, the STRING_SPLIT function returns an empty table. However, if you want to return the whole string as a single substring, you can use a combination of COALESCE and REPLACE functions to replace the missing delimiter with a custom delimiter that is guaranteed to be in the string. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1))RETURNS TABLEAS RETURN(SELECT valueFROM STRING_SPLIT(COALESCE(@string, 'N/A'), @delimiter + '##')WHERE REPLACE(value, '##', '') <> '')GOSELECT * FROM SplitString('apple,banana,orange', '|')

This will create a function SplitString that replaces the missing delimiter with '##'. The output will be:

value-----apple,banana,orange

Limiting the Number of Substrings

If you only need to split the first few substrings of a string, you can use a combination of TOP and CROSS APPLY functions to limit the number of substrings. Here’s an example:

CREATE FUNCTION SplitString(@string NVARCHAR(MAX), @delimiter NVARCHAR(1), @limit INT)RETURNS TABLEAS RETURN(SELECT TOP(@limit) valueFROM STRING_SPLIT(@string, @delimiter)CROSS APPLY (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rn) rWHERE r.rn <= @limit)GOSELECT * FROM SplitString('apple,banana,orange,kiwi', ',', 2)

This will create a function SplitString that limits the number of substrings to 2. The output will be:

value-----applebanana

Using Regular Expressions

If your string contains complex patterns that cannot be handled by simple delimiters, you can use regular expressions to split the string. SQL Server supports regular expressions through the LIKE operator and the PATINDEX function. Here’s an example:

CREATE FUNCTION SplitStringRegex(@string NVARCHAR(MAX), @pattern NVARCHAR(MAX))RETURNS TABLEAS RETURN(SELECT valueFROM (SELECT value,PATINDEX('%' + @pattern + '%', value) AS match_start,LEN(@pattern) AS match_lengthFROM STRING_SPLIT(@string, ',')) matchesWHERE match_start > 0)GOSELECT * FROM SplitStringRegex('apple1,banana2,orange3', '%[0-9]%')

This will create a function SplitStringRegex that uses the regular expression '%[