Skip to content

Instantly share code, notes, and snippets.

@cdmckay
Last active January 28, 2021 15:12
Show Gist options
  • Save cdmckay/c0237c8d89872423f660d8243e8289a9 to your computer and use it in GitHub Desktop.
Save cdmckay/c0237c8d89872423f660d8243e8289a9 to your computer and use it in GitHub Desktop.
create or replace function uuid_to_muid(id varchar)
returns varchar as
$$
trim(base64_encode(
substring(hex_decode_binary(replace(id, '-', '')), 9, 8) ||
substring(hex_decode_binary(replace(id, '-', '')), 1, 8),
0,
'-_ '
))
$$;
create or replace function muid_to_uuid(id varchar(22))
returns varchar(36) as
$$
with uuids as (select
hex_encode(substring(base64_decode_binary(id || ' ', '-_ '), 9, 9), 0) ||
hex_encode(substring(base64_decode_binary(id || ' ', '-_ '), 0, 9), 0) as uuid
)
select concat(substring(uuid, 0, 8), '-', substring(uuid, 9, 4), '-', substring(uuid, 13, 4), '-', substring(uuid, 17, 4), '-', substring(uuid, 21, 12)) from uuids
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment