The direct answer: to split values in SQL, you use the STRING_SPLIT() function in SQL Server 2016 and later, or the SPLIT_PART() function in PostgreSQL and Redshift. In MySQL, you must use a JSON-based trick or a recursive CTE, because there is no built-in split function.
Why You Need to Split Values
Databases are designed to store atomic values—one fact per column. But real-world data often arrives as a single column containing comma-separated tags, semicolon-delimited phone numbers, or pipe-separated file paths. Splitting this text into separate rows (or columns) is a common data-cleaning task. It lets you join on individual items, count occurrences, or filter for a specific element without using LIKE '%value%' (which is slow and error-prone).
How STRING_SPLIT() Works in SQL Server
In SQL Server 2016 and later (including Azure SQL Database), the simplest way is STRING_SPLIT(string, separator). It returns a single column called value with one row per item.
Example: split the string ‘apple,banana,cherry’ by comma:
SELECT value FROM STRING_SPLIT('apple,banana,cherry', ',');
Result:
value
-----
apple
banana
cherry
To split a column from a table, you apply it in a CROSS APPLY:
SELECT t.id, s.value
FROM mytable t
CROSS APPLY STRING_SPLIT(t.tags, ',') s;
Note: STRING_SPLIT() does not guarantee the order of the output rows. If you need the original position (e.g., the 2nd item), you must add an ordinal parameter, which is only available in SQL Server 2022 and Azure SQL Database. In earlier versions, you cannot get the position directly.
PostgreSQL and MySQL: Different Approaches
PostgreSQL has SPLIT_PART(string, delimiter, position) which returns a single substring at a given position. To get all pieces as rows, combine it with generate_series():
SELECT
t.id,
SPLIT_PART(t.tags, ',', n) AS item
FROM mytable t
CROSS JOIN LATERAL generate_series(1, array_length(string_to_array(t.tags, ','), 1)) AS n;
MySQL (up to version 8.0) has no built-in split function. A common workaround uses a JSON table:
SELECT t.id, j.item
FROM mytable t
CROSS JOIN JSON_TABLE(
CONCAT('[', REPLACE(t.tags, ',', '","'), ']'),
'$[*]' COLUMNS (item VARCHAR(255) PATH '$')
) AS j;
This converts the comma-separated string into a JSON array and then expands it. It works in MySQL 8.0 and MariaDB 10.5+. For older versions, you need a recursive CTE or a numbers table.
How to Choose the Right Method
Your choice depends on your database system and what you need:
- SQL Server 2016+: Use
STRING_SPLIT()—it’s fast and built-in. - SQL Server 2022+: Use
STRING_SPLIT()with theordinalparameter if you need position. - PostgreSQL: Use
SPLIT_PART()for a single piece, or a lateral join withstring_to_array()for all pieces. - MySQL 8.0+: Use
JSON_TABLE()trick—it’s clean but a bit cryptic. - MySQL 5.x / MariaDB <10.5: Use a recursive CTE or a pre-filled numbers table.
Common Problems and Misconceptions
One frequent misconception: STRING_SPLIT() returns rows in the same order as the input. It does not—unless you use the ordinal parameter in SQL Server 2022. If order matters, always capture the position.
Another issue: empty strings. If your data has consecutive delimiters (e.g., ‘apple,,cherry’), STRING_SPLIT() in SQL Server 2016–2019 skips empty values. In SQL Server 2022, you can use the enable_ordinal parameter to include them, but you still need to filter if you don’t want them. In PostgreSQL, string_to_array() preserves empty strings by default.
Performance: splitting a large table with a cross apply can be slow because the function runs per row. If you only need to filter for a single value, consider using a normalized table or a full-text index instead of splitting on every query.
Alternatives to Splitting in SQL
Splitting is often a symptom of a design problem. Better alternatives include:
- Normalize the schema: Store each item in its own row in a child table. This is the most robust solution.
- Use a JSON or XML column: Modern databases can query JSON arrays directly with functions like
JSON_VALUE()orjsonb_array_elements()in PostgreSQL. - Use a numbers table: A permanent table of integers (1–1000) can help split strings in older databases without recursive CTEs.
Frequently Asked Questions
How do I split a string into multiple columns in SQL?
To split into columns, you can use PARSENAME() in SQL Server (for up to 4 parts) or SPLIT_PART() in PostgreSQL called multiple times. For example, in SQL Server: SELECT PARSENAME(REPLACE('a-b-c', '-', '.'), 3) AS part1. In MySQL, you can use SUBSTRING_INDEX() repeatedly. These methods are limited to a fixed number of parts.
Does STRING_SPLIT() work in MySQL?
No, STRING_SPLIT() is SQL Server-specific. MySQL has no equivalent built-in function. Use the JSON_TABLE() trick (MySQL 8.0+) or a recursive CTE for older versions.
How can I get the position of each split item?
In SQL Server 2022, use STRING_SPLIT(string, ',', 1)—the third parameter enables ordinal output, giving you a ordinal column with the position. In PostgreSQL, you can use unnest(string_to_array(...)) WITH ORDINALITY. In MySQL, you can use a numbers table or JSON_TABLE() with a FOR ORDINALITY column.
Why does STRING_SPLIT() skip empty values?
In SQL Server 2016–2019, STRING_SPLIT() ignores empty strings by design. If you need to preserve them (e.g., ‘a,,b’ should return three items), upgrade to SQL Server 2022 or use a custom splitter function with a recursive CTE.
Is there a way to split a string without a function?
Yes, you can use a recursive CTE (common table expression) to walk through the string character by character. This works in any database that supports recursive CTEs (SQL Server, PostgreSQL, MySQL 8+). It’s more verbose but gives you full control over empty values and positions.
Performance and Safety Considerations
When splitting values, always test with your actual data size. STRING_SPLIT() is optimized in SQL Server, but applying it to millions of rows can still be expensive. If you’re splitting to filter rows, consider using IN with a derived table instead of splitting the column. Also, be careful with delimiters that appear in the data itself—use a delimiter that is unlikely to occur, like a pipe | or a tab character.
For security, never concatenate user input into a SQL string that you later split—this can lead to SQL injection if you’re building dynamic queries. Always use parameterized queries or stored procedures.
Related
- Split String in Power Automate: 3 Ways
- What Does String Do in Java? A Clear Guide
- Cell Counting Assay: Methods, Kits, and How to Choose
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Excel Indent Shortcut: Increase or Decrease Cell IndentIn Excel for Microsoft 365 on Windows, select the cell or cells and press Alt, H, 6 to…
- Cleaning Checklist Template Excel: Setup and Tips for TeamsA cleaning checklist template in Excel is a worksheet that lists cleaning tasks, when they are due, who…
- How to Insert a Citation Placeholder in WordTo insert a citation placeholder in Word for Microsoft 365 on Windows, place the cursor where the citation…
- Leave Calendar Template in Excel: Build or DownloadA leave calendar template in Excel can show who is away, on which dates, and whether absences overlap.…
- List of Customers in Excel: 10 Practical Customer ListsTo make a customer list in Excel, add clear column headings, enter one customer per row, then choose…
- Excel Data Table Row and Column Input ExplainedIn an Excel two-variable data table, Row input cell is the model cell replaced by each test value…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.