Oracle SQL to Sort Version Numbers

后端 未结 3 1114
有刺的猬
有刺的猬 2021-01-03 09:06

In Oracle, just using the ORDER BY does not sort version numbers. My Version_Number field is declared as a VARCHAR and I cannot change

3条回答
  •  醉酒成梦
    2021-01-03 09:35

    As Joel Coehoorn suggestions here, "refactor version number storage so that each section has it's own column: MajorVersion, MinorVersion, Revision, Build".

    I'm re-posting as I found this very helpful!

    To expand, I was looking to get the MAX version number, and ended up using this script along with Joel's suggestion.

        -- GET MAX VERSION NUMBER
        SELECT
            REPLACE(vnum, ' ', '') AS versionum
        FROM
            (SELECT
                MAX(LPAD(major, 4) || '.' || LPAD(minor, 4) || '.' || LPAD(revision, 4)) AS vnum
            FROM
                my_table
            ORDER BY
                major
              , minor
              , revision
            ) tbl1
    

提交回复
热议问题