Formating UUID String without REGEXP_REPLACE and PL/SQL












2















I'd like to format the result of the sys_guid() function such as proposed in this answer



select regexp_replace(rawtohex(sys_guid())
, '([A-F0-9]{8})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{12})'
, '1-2-3-4-5')
as FORMATTED_GUID
from dual


From performance reasons I'd like to avoid the usage of regexp_replace (as I process large number of records).



My scenario can be simplified to this use case:



 select rawtohex(sys_guid()) GUID
from dual connect by level <= 2;


Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID. I would also like to stay in SQL, without a context switch to PL/SQL function.



Any idea how to format string in SQL similar to date or number using a mask:



 to_char(rawtohex(sys_guid(),'CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC') /* note, this is clear illegal */









share|improve this question





























    2















    I'd like to format the result of the sys_guid() function such as proposed in this answer



    select regexp_replace(rawtohex(sys_guid())
    , '([A-F0-9]{8})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{12})'
    , '1-2-3-4-5')
    as FORMATTED_GUID
    from dual


    From performance reasons I'd like to avoid the usage of regexp_replace (as I process large number of records).



    My scenario can be simplified to this use case:



     select rawtohex(sys_guid()) GUID
    from dual connect by level <= 2;


    Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID. I would also like to stay in SQL, without a context switch to PL/SQL function.



    Any idea how to format string in SQL similar to date or number using a mask:



     to_char(rawtohex(sys_guid(),'CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC') /* note, this is clear illegal */









    share|improve this question



























      2












      2








      2








      I'd like to format the result of the sys_guid() function such as proposed in this answer



      select regexp_replace(rawtohex(sys_guid())
      , '([A-F0-9]{8})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{12})'
      , '1-2-3-4-5')
      as FORMATTED_GUID
      from dual


      From performance reasons I'd like to avoid the usage of regexp_replace (as I process large number of records).



      My scenario can be simplified to this use case:



       select rawtohex(sys_guid()) GUID
      from dual connect by level <= 2;


      Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID. I would also like to stay in SQL, without a context switch to PL/SQL function.



      Any idea how to format string in SQL similar to date or number using a mask:



       to_char(rawtohex(sys_guid(),'CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC') /* note, this is clear illegal */









      share|improve this question
















      I'd like to format the result of the sys_guid() function such as proposed in this answer



      select regexp_replace(rawtohex(sys_guid())
      , '([A-F0-9]{8})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{4})([A-F0-9]{12})'
      , '1-2-3-4-5')
      as FORMATTED_GUID
      from dual


      From performance reasons I'd like to avoid the usage of regexp_replace (as I process large number of records).



      My scenario can be simplified to this use case:



       select rawtohex(sys_guid()) GUID
      from dual connect by level <= 2;


      Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID. I would also like to stay in SQL, without a context switch to PL/SQL function.



      Any idea how to format string in SQL similar to date or number using a mask:



       to_char(rawtohex(sys_guid(),'CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC') /* note, this is clear illegal */






      string oracle format uuid






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 11:29







      Marmite Bomber

















      asked Apr 1 '16 at 10:56









      Marmite BomberMarmite Bomber

      8,01231033




      8,01231033
























          1 Answer
          1






          active

          oldest

          votes


















          4














          You can't include string literals in number formats unfortunately, otherwise you could convert the hex string to a number and then back again, inserting literals in the format mask in the right places - but you can only do that for dates.



          You can use substr() since the positions are fixed. You were concerned that




          Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID.




          Using subquery factoring (a.ka. a common table expression/CTE) means the substr() calls for a row from that CTE all see the same GUID; this method doesn't generate a new SYS_GUID for each one.



          with t as (
          select rawtohex(sys_guid()) guid from dual
          connect by level <= 2
          )
          select guid, substr(guid, 1, 8)
          ||'-'|| substr(guid, 9, 4)
          ||'-'|| substr(guid, 13, 4)
          ||'-'|| substr(guid, 17, 4)
          ||'-'|| substr(guid, 21, 12) as formatted_guid
          from t;

          GUID FORMATTED_GUID
          -------------------------------- ----------------------------------------
          2F6BA62518F926D0E0534D49E50ABB46 2F6BA625-18F9-26D0-E053-4D49E50ABB46
          2F6BA62518FA26D0E0534D49E50ABB46 2F6BA625-18FA-26D0-E053-4D49E50ABB46


          That's a lot faster than the regex on a larger amount of data. With 100000 values in a loop (in a PL/SQL block, doing a minimal amount of work inside the loop to make it actually evaluate properly, and using dbms_utility.get_cpu_time to check the elapsed time) the regex version takes about 2.51 seconds, while the substring version takes about 0.29 seconds. Your system will get different numbers of course, but it should still be about the same order of magnitude.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36354555%2fformating-uuid-string-without-regexp-replace-and-pl-sql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            You can't include string literals in number formats unfortunately, otherwise you could convert the hex string to a number and then back again, inserting literals in the format mask in the right places - but you can only do that for dates.



            You can use substr() since the positions are fixed. You were concerned that




            Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID.




            Using subquery factoring (a.ka. a common table expression/CTE) means the substr() calls for a row from that CTE all see the same GUID; this method doesn't generate a new SYS_GUID for each one.



            with t as (
            select rawtohex(sys_guid()) guid from dual
            connect by level <= 2
            )
            select guid, substr(guid, 1, 8)
            ||'-'|| substr(guid, 9, 4)
            ||'-'|| substr(guid, 13, 4)
            ||'-'|| substr(guid, 17, 4)
            ||'-'|| substr(guid, 21, 12) as formatted_guid
            from t;

            GUID FORMATTED_GUID
            -------------------------------- ----------------------------------------
            2F6BA62518F926D0E0534D49E50ABB46 2F6BA625-18F9-26D0-E053-4D49E50ABB46
            2F6BA62518FA26D0E0534D49E50ABB46 2F6BA625-18FA-26D0-E053-4D49E50ABB46


            That's a lot faster than the regex on a larger amount of data. With 100000 values in a loop (in a PL/SQL block, doing a minimal amount of work inside the loop to make it actually evaluate properly, and using dbms_utility.get_cpu_time to check the elapsed time) the regex version takes about 2.51 seconds, while the substring version takes about 0.29 seconds. Your system will get different numbers of course, but it should still be about the same order of magnitude.






            share|improve this answer






























              4














              You can't include string literals in number formats unfortunately, otherwise you could convert the hex string to a number and then back again, inserting literals in the format mask in the right places - but you can only do that for dates.



              You can use substr() since the positions are fixed. You were concerned that




              Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID.




              Using subquery factoring (a.ka. a common table expression/CTE) means the substr() calls for a row from that CTE all see the same GUID; this method doesn't generate a new SYS_GUID for each one.



              with t as (
              select rawtohex(sys_guid()) guid from dual
              connect by level <= 2
              )
              select guid, substr(guid, 1, 8)
              ||'-'|| substr(guid, 9, 4)
              ||'-'|| substr(guid, 13, 4)
              ||'-'|| substr(guid, 17, 4)
              ||'-'|| substr(guid, 21, 12) as formatted_guid
              from t;

              GUID FORMATTED_GUID
              -------------------------------- ----------------------------------------
              2F6BA62518F926D0E0534D49E50ABB46 2F6BA625-18F9-26D0-E053-4D49E50ABB46
              2F6BA62518FA26D0E0534D49E50ABB46 2F6BA625-18FA-26D0-E053-4D49E50ABB46


              That's a lot faster than the regex on a larger amount of data. With 100000 values in a loop (in a PL/SQL block, doing a minimal amount of work inside the loop to make it actually evaluate properly, and using dbms_utility.get_cpu_time to check the elapsed time) the regex version takes about 2.51 seconds, while the substring version takes about 0.29 seconds. Your system will get different numbers of course, but it should still be about the same order of magnitude.






              share|improve this answer




























                4












                4








                4







                You can't include string literals in number formats unfortunately, otherwise you could convert the hex string to a number and then back again, inserting literals in the format mask in the right places - but you can only do that for dates.



                You can use substr() since the positions are fixed. You were concerned that




                Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID.




                Using subquery factoring (a.ka. a common table expression/CTE) means the substr() calls for a row from that CTE all see the same GUID; this method doesn't generate a new SYS_GUID for each one.



                with t as (
                select rawtohex(sys_guid()) guid from dual
                connect by level <= 2
                )
                select guid, substr(guid, 1, 8)
                ||'-'|| substr(guid, 9, 4)
                ||'-'|| substr(guid, 13, 4)
                ||'-'|| substr(guid, 17, 4)
                ||'-'|| substr(guid, 21, 12) as formatted_guid
                from t;

                GUID FORMATTED_GUID
                -------------------------------- ----------------------------------------
                2F6BA62518F926D0E0534D49E50ABB46 2F6BA625-18F9-26D0-E053-4D49E50ABB46
                2F6BA62518FA26D0E0534D49E50ABB46 2F6BA625-18FA-26D0-E053-4D49E50ABB46


                That's a lot faster than the regex on a larger amount of data. With 100000 values in a loop (in a PL/SQL block, doing a minimal amount of work inside the loop to make it actually evaluate properly, and using dbms_utility.get_cpu_time to check the elapsed time) the regex version takes about 2.51 seconds, while the substring version takes about 0.29 seconds. Your system will get different numbers of course, but it should still be about the same order of magnitude.






                share|improve this answer















                You can't include string literals in number formats unfortunately, otherwise you could convert the hex string to a number and then back again, inserting literals in the format mask in the right places - but you can only do that for dates.



                You can use substr() since the positions are fixed. You were concerned that




                Obviously I can't use substr and concatenation as each SUBSTR would process a different SYS_GUID.




                Using subquery factoring (a.ka. a common table expression/CTE) means the substr() calls for a row from that CTE all see the same GUID; this method doesn't generate a new SYS_GUID for each one.



                with t as (
                select rawtohex(sys_guid()) guid from dual
                connect by level <= 2
                )
                select guid, substr(guid, 1, 8)
                ||'-'|| substr(guid, 9, 4)
                ||'-'|| substr(guid, 13, 4)
                ||'-'|| substr(guid, 17, 4)
                ||'-'|| substr(guid, 21, 12) as formatted_guid
                from t;

                GUID FORMATTED_GUID
                -------------------------------- ----------------------------------------
                2F6BA62518F926D0E0534D49E50ABB46 2F6BA625-18F9-26D0-E053-4D49E50ABB46
                2F6BA62518FA26D0E0534D49E50ABB46 2F6BA625-18FA-26D0-E053-4D49E50ABB46


                That's a lot faster than the regex on a larger amount of data. With 100000 values in a loop (in a PL/SQL block, doing a minimal amount of work inside the loop to make it actually evaluate properly, and using dbms_utility.get_cpu_time to check the elapsed time) the regex version takes about 2.51 seconds, while the substring version takes about 0.29 seconds. Your system will get different numbers of course, but it should still be about the same order of magnitude.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 1 '16 at 11:53

























                answered Apr 1 '16 at 11:47









                Alex PooleAlex Poole

                132k6105178




                132k6105178
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36354555%2fformating-uuid-string-without-regexp-replace-and-pl-sql%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'