Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Created February 14, 2024 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiolimace/e3c3d354d1afe0b3175f65be2d962523 to your computer and use it in GitHub Desktop.
Save fabiolimace/e3c3d354d1afe0b3175f65be2d962523 to your computer and use it in GitHub Desktop.
UUIDv7 for SQLite
-- UUIDv7 canonical format: AAAAAAAA-BBBB-7CCC-DDDD-EEEEEEEEEEEE
select printf('%08x', ((strftime('%s') * 1000) >> 16)) || '-' ||
printf('%04x', ((strftime('%s') * 1000) + ((strftime('%f') * 1000) % 1000)) & 0xffff) || '-' ||
printf('%04x', 0x7000 + abs(random()) % 0x0fff) || '-' ||
printf('%04x', 0x8000 + abs(random()) % 0x3fff) || '-' ||
printf('%012x', abs(random()) >> 16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment