SQL String Split Example: How to Split Strings in SQL

How-to basics · Updated November 23, 2026

The simplest way to split a string in SQL is to use the STRING_SPLIT function in SQL Server 2016 and later, or the SPLIT_PART function in PostgreSQL. For example, in SQL Server, SELECT value FROM STRING_SPLIT('apple,banana,cherry', ','); returns three rows: apple, banana, and cherry. This article explains the most common methods across major databases, how they differ, and how to handle the limitations you’ll hit in real queries.

Why Splitting Strings in SQL Is Tricky

SQL is designed to work with rows and columns, not lists inside a single field. When you store a comma-separated value like 'red,green,blue' in one column, you break the relational model—each value should ideally be its own row. But legacy data, user input, or configuration tables often force you to split strings anyway. The challenge is that every major database has its own function, syntax, and quirks. Unlike Excel’s Text to Columns or a programming language’s split(), SQL has no universal standard. You must know which database you’re using before you write the query.

SQL Server: STRING_SPLIT and Its Limits

SQL Server 2016 introduced STRING_SPLIT. It takes two arguments: the string and the delimiter. It returns a single column named value.

SELECT value FROM STRING_SPLIT('apple,banana,cherry', ',');

Result:

value
-----
apple
banana
cherry

Important limitations:

  • It only accepts a single-character delimiter. You cannot split on a multi-character string like '||' without extra work.
  • It doesn’t return an ordinal (position) column in versions before SQL Server 2022. If you need the order (e.g., ‘first item’ vs ‘second’), you must use a different approach or upgrade to 2022, which adds an ordinal column.
  • Empty values are omitted by default. STRING_SPLIT('a,,b', ',') returns only ‘a’ and ‘b’. To keep empty strings, you need the enable_ordinal parameter in SQL Server 2022 or use a JSON-based workaround.

For example, to get the position in SQL Server 2022:

SELECT value, ordinal FROM STRING_SPLIT('apple,banana,cherry', ',', 1);

This returns ordinal 1, 2, 3 for each fruit.

PostgreSQL: SPLIT_PART and string_to_array

PostgreSQL has two main functions. SPLIT_PART(string, delimiter, position) returns a single substring at the given position.

SELECT SPLIT_PART('apple,banana,cherry', ',', 2);

Returns 'banana'. The position starts at 1. This is handy when you only need one specific element.

If you need all elements as rows, use string_to_array with unnest:

SELECT unnest(string_to_array('apple,banana,cherry', ','));

This returns three rows. Unlike SQL Server, string_to_array can handle multi-character delimiters: string_to_array('a||b', '||') works fine. It also preserves empty strings, so string_to_array('a,,b', ',') returns three elements: ‘a’, ”, ‘b’.

MySQL: No Built-In Split Function

MySQL does not have a dedicated split function. The typical workaround is to use a recursive CTE (Common Table Expression) or a JSON table. Here’s a recursive CTE that works in MySQL 8.0+:

WITH RECURSIVE split AS (
  SELECT 'apple,banana,cherry' AS str, 1 AS pos
  UNION ALL
  SELECT SUBSTRING_INDEX(str, ',', 1),
         LOCATE(',', str) + 1
  FROM split
  WHERE LOCATE(',', str) > 0
)
SELECT SUBSTRING_INDEX(str, ',', 1) AS value FROM split;

This is more complex and can be slow on large strings. A simpler but less flexible method is JSON_TABLE:

SELECT jt.value
FROM JSON_TABLE(
  CONCAT('["', REPLACE('apple,banana,cherry', ',', '","'), '"]'),
  '$[*]' COLUMNS (value VARCHAR(255) PATH '$')
) AS jt;

This converts the comma-separated string into a JSON array and then unpivots it. It works in MySQL 8.0 and is often faster than a recursive CTE for long strings.

Oracle and SQLite: Different Approaches

Oracle has REGEXP_SUBSTR with a pattern that extracts each element. For example, to get the third element:

SELECT REGEXP_SUBSTR('apple,banana,cherry', '[^,]+', 1, 3) FROM dual;

To get all rows, you can use a lateral join with a level generator:

SELECT REGEXP_SUBSTR('apple,banana,cherry', '[^,]+', 1, LEVEL) AS value
FROM dual
CONNECT BY LEVEL <= REGEXP_COUNT('apple,banana,cherry', ',') + 1;

SQLite doesn’t have a split function either. The common method is to use a recursive CTE similar to MySQL, or to use the json_each function if you convert the string to a JSON array:

SELECT value FROM json_each('["apple","banana","cherry"]');

But that requires you to manually format the JSON, which is not practical for dynamic strings. The recursive CTE is the standard solution.

How to Choose the Right Method

Here’s a quick decision guide:

  • SQL Server 2016+: Use STRING_SPLIT unless you need ordinal positions (then use SQL Server 2022’s enable_ordinal).
  • PostgreSQL: Use unnest(string_to_array(...)) for all rows, or SPLIT_PART for a single element.
  • MySQL 8.0+: Use JSON_TABLE for simplicity and performance, or a recursive CTE if you need to avoid JSON conversion.
  • Oracle: Use REGEXP_SUBSTR with a connect-by level.
  • SQLite: Use a recursive CTE (or a custom function if you’re using a programming language wrapper).

Also consider whether you need to keep empty strings. SQL Server’s STRING_SPLIT drops them by default; PostgreSQL’s string_to_array keeps them; MySQL’s JSON_TABLE drops them unless you handle the REPLACE carefully.

Common Problems and Misconceptions

One common mistake is assuming STRING_SPLIT returns the order of elements. It does not guarantee order unless you use the ordinal column. If you rely on position, you’ll get unpredictable results.

Another issue: splitting a string that contains the delimiter inside quotes, like '"apple,banana",cherry'. None of the built-in functions handle quoted delimiters. You’d need a custom parser or a more advanced regex.

Performance can also be a problem. Splitting a string in SQL is often slower than doing it in the application layer. If you need to split millions of rows, consider normalizing your schema or using a dedicated splitter table.

Alternatives to SQL String Splitting

If you have control over the data, the best alternative is to store each item in its own row. Use a junction table for many-to-many relationships. If you must store delimited data, consider using JSON or XML columns instead—they have built-in query functions in most databases (e.g., JSON_VALUE in SQL Server, jsonb in PostgreSQL).

In Excel, you’d use Text to Columns or FILTERXML in older versions, but in SQL, the functions above are the standard. For ad-hoc analysis, you might also export to a tool like Python or R, but that’s outside SQL.

Frequently Asked Questions

How do I split a string into multiple rows in SQL Server without STRING_SPLIT?

If you’re on an older version (pre-2016), you can use a recursive CTE or a numbers table. For example, with a numbers table (a table of integers 1 to N), you can extract each substring with SUBSTRING and CHARINDEX. But if you can, upgrade or use a CLR function.

Does STRING_SPLIT work in SQL Server 2019?

Yes, STRING_SPLIT is available in SQL Server 2016, 2017, 2019, and 2022. The only difference is that 2022 adds the optional ordinal parameter.

How do I split a string by multiple delimiters?

None of the built-in functions support multiple delimiters directly. You can replace all delimiters with a single one first, e.g., in SQL Server: REPLACE(REPLACE(myString, ';', ','), '|', ','), then split on the comma. In PostgreSQL, you can use a regex split: regexp_split_to_table(myString, '[,;|]').

Can I split a string in SQL without a function?

Yes, you can use a recursive CTE or a numbers table to manually parse the string. This works in any database that supports recursive CTEs (MySQL 8+, PostgreSQL, SQL Server 2005+). It’s more verbose but doesn’t depend on a specific built-in function.

Why does STRING_SPLIT ignore empty values?

By default, STRING_SPLIT omits empty substrings. This is documented behavior. If you need to keep them, you can use the enable_ordinal parameter in SQL Server 2022, or use a workaround like replacing empty strings with a placeholder before splitting, then filtering after.

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.