sqlsnowflake-cloud-data-platformsql-order-byversion

How to sort "version" strings with SQL in Snowflake?


I have a column with strings representing release "versions", like:

5.14.0
5.10.1
5.1.8
5.1.87
5.1.357
26.3.0
24.1.1

How can I sort these in Snowflake? Note that the numeric sort (what we want) is different than the simple string sort.


Solution

  • We discussed this on the dbt Slack - my suggestion moves the sorting transformations into the order by - by splitting the string and converting it into an array of integers. Snowflake natively knows how to sort these arrays in the correct order:

    select *
    from data
    order by (
        select [y[0]::int, y[1]::int, y[2]::int]
        from(
            select split(version_number, '.') y
        )
    );
    

    Sample data:

    create table data as 
    select value version_number
    from (table(split_to_table(
    $$5.14.0
    5.10.1
    5.1.8
    5.1.87
    5.1.357
    26.3.0
    24.1.1$$, '\n'
        )))
    ;
    

    We can also create a UDF for reusability:

    create function string_to_int_array_3(version_number string)
    returns array
    as
    $$
        select [y[0]::int, y[1]::int, y[2]::int] 
        from(
            select split(version_number, '.') y
        )
    $$
    ;
    
    select *
    from data
    order by string_to_int_array_3(version_number);
    

    enter image description here