Categories: Varia

New Argument for STRING_SPLIT Function

A new (optional) argument has been added to STRING_SPLIT: enable_ordinal. Setting it to 1 will add an output column with the 1-based index of each item of the array. Nice. This is great if you want to preserve the order of the items. Let’s illustrate with an example:

SELECT *
FROM STRING_SPLIT('Hello,World,this,is,an,array',',');

This returns the following result:

As you can see, the order is the same as in the input string, but this is not guaranteed! It’s exactly the same as relying on indexes for your sorted output: not explicitly guaranteed until you use an ORDER BY. The tricky thing with this function is, there’s nothing to sort on if you explicitly want to reserve the order. To work around this, I typically used a user-defined function written by Jeff Moden, as explained in this infamous article about tally tables: Tally OH! An Improved SQL 8K “CSV Splitter” Function. Jeff’s function returns an ordinal column and thus you can sort the output.

Microsoft has now added such an ordinal column as well to the built-in STRING_SPLIT function. Hoozah.

SELECT *
FROM STRING_SPLIT('Hello,World,this,is,an,array',',',1)
ORDER BY ordinal;

The sad part is that this change is for the moment only available in the Azure databases (Azure SQL DB, Azure Managed Instance and Azure Synapse Analytics Serverless).

Aaron Bertrand was the first to break the news (at least that I’m aware of) in his blog post 2021 : The Year of the Exodus, but since it was kind of a footnote and I thought it deserved it’s own post 🙂


------------------------------------------------
Do you like this blog post? You can thank me by buying me a beer 🙂
Koen Verbeeck

Koen Verbeeck is a Microsoft Business Intelligence consultant at AE, helping clients to get insight in their data. Koen has a comprehensive knowledge of the SQL Server BI stack, with a particular love for Integration Services. He's also a speaker at various conferences.

View Comments

  • Thanks for the honorable mention, Koen.

    The really sad part about all of this is them originally releasing the function without this feature. It should have been included since day 1.

Recent Posts

Free webinar – Tackling the Gaps and Islands Problem with T-SQL Window Functions

I'm hosting a free webinar at MSSQLTips.com at the 19th of December 2024, 6PM UTC.…

5 days ago

dataMinds Connect 2024 – Session Materials

The slides and scripts for my session "Tackling the Gaps & Islands Problem with T-SQL…

4 weeks ago

Connect to Power BI as a Guest User in another Tenant

Sometimes your Microsoft Entra ID account (formerly known as Azure Active Directory) is added as…

2 months ago

How to use a Script Activity in ADF as a Lookup

In Azure Data Factory (ADF, but also Synapse Pipelines and Fabric Pipelines), you have a…

4 months ago

Database Build Error – Incorrect syntax near DISTINCT

I wrote a piece of SQL that had some new T-SQL syntax in it: IS…

4 months ago

Speaking at dataMinds Connect 2024

I'm very excited to announce I've been selected as a speaker for dataMinds Connect 2024,…

5 months ago