0

I need to create UUIDs version 4 in the format like xxxxxxxxxxxx-4xxx-yxxx-xxxxxxxxxxxx. (x= any hexadecimal character (lower case only); y is one of 8, 9, a or b; 4 is static)

The best solution for me to do this is in my Oracle database. After some research I found out that SYS_GUID() can be a solution:

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 UUID from dual;

But the result is not what I expect:

UUID
------------------------------------
712EE4F7-823E-AD73-E053-8E3A220A3EFB

So the "4" at position 13 and "y" at position 17 are not correct in my example. Any other/better ways to create an UUID or why is SYS_GUID returning an other format?

ZerOne
  • 1,296
  • 6
  • 20
  • 40
  • 1
    [`sys_guid()` doesn't seem to be RFC-compliant](https://stackoverflow.com/q/6505837/266304), but doesn't claim to be. I was going to write up a Java stored procedure but [found this that already does it](https://stackoverflow.com/a/13966531/266304). If that does what you want then this can be closed as a duplicate. – Alex Poole Jul 17 '18 at 09:27
  • 1
    @AlexPoole yeah I think you are right, there is no better solution. this can be closed – ZerOne Jul 17 '18 at 09:31

0 Answers0