SQL Split: How to Split Strings in SQL Queries

How-to basics · Updated November 22, 2026

To split a string in a SQL query, you use a string splitting function that breaks a delimited string into rows or parts. The exact function depends on your database: STRING_SPLIT() in SQL Server, SPLIT_PART() in PostgreSQL, SUBSTRING_INDEX() in MySQL, and REGEXP_SPLIT_TO_TABLE() in PostgreSQL for regex splitting. For example, in SQL Server, SELECT value FROM STRING_SPLIT('apple,banana,cherry', ',') returns three rows.

Why Splitting Strings in SQL Is Tricky

SQL is designed for structured, tabular data, not for parsing delimited text. Storing multiple values in a single column (like “red,green,blue”) violates first normal form and makes queries awkward. Splitting is often needed when you inherit a legacy database, import CSV data, or parse log files. The challenge is that SQL’s set-based logic doesn’t naturally handle “iterating” over a string’s characters. Older solutions used recursive CTEs, number tables, or XML tricks—each with performance or complexity drawbacks. Modern databases provide built-in functions that simplify this, but they differ in syntax and behavior.

Built-in Split Functions by Database

Here are the most common built-in functions for splitting strings into rows:

  • SQL Server (2016+): STRING_SPLIT(string, delimiter) returns a single column named value. It does not return an ordinal position by default (though you can add ordinal with ENABLE_ORDINAL in SQL Server 2022+).
  • PostgreSQL: SPLIT_PART(string, delimiter, position) returns the part at a given position (1-based). For all parts as rows, use regexp_split_to_table(string, pattern).
  • MySQL: No native split-to-rows function. Use SUBSTRING_INDEX(string, delimiter, count) to extract a specific part, or use a JSON_TABLE trick with JSON_EXTRACT (MySQL 8.0+).
  • SQLite: No built-in split function. Use recursive CTE or a custom function.
  • Oracle: REGEXP_SUBSTR(string, '[^,]+', 1, level) with a CONNECT BY query.

How to Split a String into Rows (Examples)

Let’s look at concrete examples for the most common databases.

SQL Server: To split and use the parts in a query:

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

Result: three rows: apple, banana, cherry.

PostgreSQL: To get all parts as rows:

SELECT regexp_split_to_table('apple,banana,cherry', ',');

To get a specific part (e.g., the 2nd):

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

Returns ‘banana’.

MySQL 8.0+: Using JSON_TABLE:

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

This is verbose but works. Alternatively, use a recursive CTE (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), pos+1 FROM split WHERE str LIKE '%,%') SELECT SUBSTRING_INDEX(str, ',', 1) AS part FROM split;

Note: this example is simplified and may need adjustment for edge cases.

How to Choose the Right Split Method

Consider these factors when picking a split method:

  • Database version: Use built-in functions if available (SQL Server 2016+, PostgreSQL 9.1+ for regexp_split_to_table, MySQL 8.0+ for JSON_TABLE).
  • Need for position/order: If you need the index of each part, SQL Server 2022’s ordinal or PostgreSQL’s regexp_split_to_table with WITH ORDINALITY can help.
  • Performance: Built-in functions are usually faster than recursive CTEs or XML methods. For large datasets, test with your data.
  • Delimiter complexity: For simple commas, use simple functions. For regex patterns (e.g., splitting on multiple characters), use regexp_split_to_table in PostgreSQL or REGEXP_SUBSTR in Oracle.

Common Problems and Pitfalls

Splitting strings can lead to several issues:

  • Empty parts: STRING_SPLIT in SQL Server ignores empty strings by default (e.g., ‘a,,b’ returns only ‘a’ and ‘b’). If you need to preserve empty parts, you must use a different method or a custom function.
  • Trailing delimiters: ‘a,b,’ may produce an empty last part or ignore it depending on the function.
  • NULL input: Most functions return NULL or no rows when input is NULL. Handle with COALESCE or WHERE filters.
  • Delimiter in data: If your data contains the delimiter as part of a value (e.g., “Smith, John”), splitting naively will break it. Consider using a different delimiter or escaping.
  • Performance on large strings: Recursive CTEs can be slow for long strings. Use built-in functions or a numbers table.

Alternatives to Splitting in SQL

Sometimes splitting is not the best approach:

  • Normalize the schema: Store each value in its own row in a related table. This is the proper relational design.
  • Use JSON/XML: Store structured data as JSON or XML and query it with native functions (e.g., JSON_VALUE, OPENJSON in SQL Server, jsonb in PostgreSQL).
  • Do the split in application code: If you’re using a programming language, split the string there and pass the parts as parameters (e.g., using a table-valued parameter in SQL Server).
  • Use a numbers table: A permanent table of numbers (1 to 1,000,000) can be used to split strings via SUBSTRING and CHARINDEX. This works in any database but requires maintenance.

Frequently Asked Questions

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

If you are on SQL Server 2016 or later, use STRING_SPLIT. For older versions, you can use a recursive CTE or a numbers table. For example, a recursive CTE that loops through the string and extracts each part. Another common method is to use XML: SELECT value FROM (SELECT CAST('<M>' + REPLACE(@string, ',', '</M><M>') + '</M>' AS XML) AS x) t CROSS APPLY x.nodes('/M') AS n(value). This works but has performance and escaping issues.

Does STRING_SPLIT return the position of each element?

In SQL Server 2016-2019, STRING_SPLIT does not return a position. Starting with SQL Server 2022, you can use the optional ordinal parameter: SELECT * FROM STRING_SPLIT('a,b,c', ',', 1) returns a column named ordinal with values 1, 2, 3. For earlier versions, you can use a numbers table or a recursive CTE to simulate positions.

Can I split a string into columns instead of rows?

Yes, but it’s less common. In SQL Server, you can use PARSENAME for up to four parts: SELECT PARSENAME('apple.banana.cherry', 2) returns ‘banana’. For more parts, use SUBSTRING with CHARINDEX to extract each column. In PostgreSQL, you can use split_part for each column: SELECT split_part('a,b,c', ',', 1) AS col1, split_part('a,b,c', ',', 2) AS col2. This is fine when you know the exact number of parts.

Why does STRING_SPLIT ignore empty values?

By design, STRING_SPLIT filters out empty strings. For example, STRING_SPLIT('a,,b', ',') returns only ‘a’ and ‘b’. If you need to keep empty values, you can replace empty strings with a placeholder before splitting, or use a custom split function. For instance, replace ‘,,’ with ‘,NULL,’ and then interpret NULL as empty.

Is there a performance difference between splitting methods?

Yes. Built-in functions like STRING_SPLIT are highly optimized and generally faster than recursive CTEs or XML methods. For large datasets (millions of rows), the difference can be significant. Recursive CTEs can be slow because they iterate row-by-row. XML methods add parsing overhead and can break with special characters. Always test with your actual data volume.

How do I split a string in MySQL?

MySQL does not have a built-in split-to-rows function. For MySQL 8.0+, you can use JSON_TABLE as shown above, or a recursive CTE. For older versions, you can use SUBSTRING_INDEX in a loop with a numbers table. A common workaround is to use a stored procedure or a user-defined function. For simple cases, SUBSTRING_INDEX can extract a specific part: SELECT SUBSTRING_INDEX('a,b,c', ',', 1) returns ‘a’, and SUBSTRING_INDEX(SUBSTRING_INDEX('a,b,c', ',', 2), ',', -1) returns ‘b’.

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.