What is the purpose of the STRING_SPLIT function in SQL?

Learn SQL with Udemy

For an instructor lead, in-depth look at learning SQL click below.


The STRING_SPLIT function in SQL Server is a table-valued function that splits a string into rows of substrings, based on a specified separator. Introduced in SQL Server 2016 (13.x), it has proven to be a handy tool when it comes to dealing with string manipulation tasks.

Syntax

The basic syntax of STRING_SPLIT function in SQL is as follows:

How does STRING_SPLIT work?

STRING_SPLIT function takes two parameters:

  1. string: This is the original string which is needed to be split.
  2. separator: This is the delimiter based on which the string will be split.

The function will return a single-column table, with a row for each substring.

Example

Consider the following example where we have a single comma-separated string:

If we wish to split this string based on the comma as a delimiter, we could use the following SQL:

As a result, the SQL Server will return a table that looks something like this:

value
SQL
Server
STRING_SPLIT
Function

In conclusion, the STRING_SPLIT function in SQL Server is a simple yet powerful function that can assist you with string manipulation tasks involving split operations on strings.

Leave a Comment