Knowledge base / SQL

SQL Server STRING_SPLIT: order, empty values and APPLY

I split a list and a table column, then explain why token positions and expanded row counts affect the meaning of an analysis.

Kajo RudzińskiPublished Updated 4 min read

A machine separates a strip into individual blocks.

STRING_SPLIT separates text at a single delimiter character and returns a table of fragments. I use it for simple lists already supplied in that form. Before splitting, I check whether a comma always separates elements. This function does not interpret quoted fields according to the rules of a complete CSV format.

Version requirements and the two calls

This is a T-SQL function in SQL Server, not a shared function name across all databases. The examples assume SQL Server 2016 or later and database compatibility level at least 130. The ordinal variant requires SQL Server 2022 or later, and is also available in the Azure services listed in Microsoft’s documentation.

SELECT value
FROM STRING_SPLIT('red,blue,red', ',');

The result contains three elements: red, blue and red. Duplicates remain, and row order is not guaranteed. When input position matters, I enable the position column with a constant third argument in SQL Server 2022+, then explicitly sort the output.

SELECT value, ordinal
FROM STRING_SPLIT('red,blue,red', ',', 1)
ORDER BY ordinal;
value ordinal
red 1
blue 2
red 3

Positions start at 1. Adding ordinal does not itself order the displayed rows, so ORDER BY remains necessary. I do not use WITH ORDINALITY, which is not the syntax of this T-SQL call. Nor do I change database configuration merely to follow a text splitting lesson.

Empty fragments, spaces and NULL

All example data is synthetic. In red,,blue, consecutive commas create an empty fragment. If the task permits removing it, I state that decision explicitly:

SELECT value
FROM STRING_SPLIT('red,,blue', ',')
WHERE value <> '';

This returns red and blue, without an established order. Removing the empty fragment is a data treatment choice rather than a repair of the original record. If an empty field represents a missing survey answer, discarding it may change the interpretation. I establish its meaning before cleaning it away.

The delimiter is one character. Splitting red, blue at the comma leaves a leading space on the second fragment. I cannot pass ', ' as a two character delimiter. If spaces are irrelevant, a separate documented cleaning step is needed. A NULL input produces an empty table, so an absent list and a list containing an empty element deserve separate consideration.

Split a column with CROSS APPLY

When each source record contains a list, I use CROSS APPLY. The FROM and APPLY documentation describes evaluating the right source for each row of the left source. This example creates no persistent tables.

SELECT p.id, s.value AS tag
FROM (VALUES
    (1, 'sql,bi'),
    (2, 'sql'),
    (3, CAST(NULL AS varchar(20)))
) AS p(id, tags)
CROSS APPLY STRING_SPLIT(p.tags, ',') AS s
ORDER BY p.id, s.value;
id tag
1 bi
1 sql
2 sql

The first source row produces two rows, the second produces one, and the third disappears because splitting NULL produces none. If I need to retain a record without tags, I consider OUTER APPLY and explicitly define how its missing values should be treated. I do not invent an empty tag or zero without deciding what it means.

Separated fragments enter compartments that preserve their positions in a sequence.

A list of identifiers is still text

The returned value fragments have a text type. For numeric identifiers, I make conversion explicit. TRY_CONVERT returns NULL when a permitted conversion fails for a value. That does not mean an invalid identifier should disappear without explanation.

SELECT value AS raw_id, TRY_CONVERT(int, value) AS id
FROM STRING_SPLIT('10,x,20', ',')
ORDER BY value;

The fragment 10 converts to integer 10, 20 converts to 20, and x has no converted integer value. I retain raw_id so that the problem remains explainable. Before matching the list to a products table, I decide whether an invalid element should stop the task or be returned in a separate list of rejected values.

Avoid multiplying the original measures

I do not blindly sum a source record’s amount after expanding its tags. A record containing 100 and two tags produces two rows containing 100 each. Summing them gives 200 because the record was repeated. If the question is only whether a tag exists, an existence condition may be more suitable than expansion. My SQL COUNT entry discusses the importance of identifying what a row count represents.

I do not use STRING_AGG to parse XML. STRING_AGG joins values into text; it is not a parser or replacement for splitting. If the source is full CSV or needs a delimiter containing multiple characters, choose a tool that supports that format. A simple comma split should not be presented as equivalent handling.

For practice, follow the list a,b,a. How many rows appear, how many distinct values exist, and what information disappears if duplicates are removed? Then ask whether order and repeated values matter in your real task. Those answers should guide the splitting, filtering and matching operations you choose.

Key takeaways

When did SQL Server add ordinal?

SQL Server 2022 introduced the optional third argument. Pass 1 and use ORDER BY ordinal to display input order.

Does STRING_SPLIT remove duplicates?

No. Repeated fragments remain separate rows. Removing duplicates requires an explicit decision and operation.

Is STRING_SPLIT a CSV parser?

No. It splits at one character and does not interpret CSV field quoting rules.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.