// conversion reference

SQL SYS_GUID function

SELECT SYS_GUID() FROM dual returns a unique 16-byte RAW value. It is not a standard UUID, and consecutive calls often differ in only a few bytes.

Kategorie
Conversion
Returns
raw(16)
In this category
34 of 39

Syntax

SYS_GUID()

Engine-Unterstützung

EngineSupportNotizen
SQL Server Different name or syntax NEWID() is the string-form twin
PostgreSQL Different name or syntax gen_random_uuid() is the string-form twin
MySQL Different name or syntax UUID() is the string-form twin
SQLite Not built in no built-in; hex(randomblob(16))
Oracle Ja Supported under this name.

Beispiele

Oracle

SELECT SYS_GUID() FROM dual;

Oracle

SELECT RAWTOHEX(SYS_GUID()) AS guid_text FROM dual;

Oracle

CREATE TABLE sessions (
  id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
  label VARCHAR2(100)
);

Gut zu wissen

  • The value is RAW(16) bytes, not a 36-character string: RAWTOHEX gives 32 hex characters, or convert in the application when an interchange format is needed.
  • On many platforms SYS_GUID values are close to sequential: three calls in a row can come back as ...1F4C054F..., ...1F4D054F..., ...1F4E054F.... They are unique but guessable, so never use them as tokens or secrets.
  • Oracle 23ai release update 23.9 adds UUID(), which returns a random version 4 UUID as RAW(16), and RAW_TO_UUID to print it with dashes. Prefer it over SYS_GUID when you need a real UUID.

Questions about SYS_GUID

How do I get a standard UUID string?

Format the hex: REGEXP_REPLACE(RAWTOHEX(SYS_GUID()), '(.{8})(.{4})(.{4})(.{4})(.{12})', '\1-\2-\3-\4-\5'). The result has UUID shape but no version bits. On 23.9 and later, RAW_TO_UUID(UUID()) returns a real v4 UUID string directly.

Are SYS_GUID values random?

No. Oracle documents them as unique, built from a host identifier and a counter on most platforms, which is why neighbouring values share most of their bytes. Use DBMS_CRYPTO.RANDOMBYTES, or UUID() on 23.9 and later, for randomness.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.