Formating UUID String without REGEXP_REPLACE and PL/SQL
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
add a comment |
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
add a comment |
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
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
string oracle format uuid
edited Nov 25 '18 at 11:29
Marmite Bomber
asked Apr 1 '16 at 10:56
Marmite BomberMarmite Bomber
8,01231033
8,01231033
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Apr 1 '16 at 11:53
answered Apr 1 '16 at 11:47
Alex PooleAlex Poole
132k6105178
132k6105178
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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