Skip to content

Instantly share code, notes, and snippets.

@technetium
Last active May 27, 2024 17:26
Show Gist options
  • Save technetium/549917077aa690a0d2f254ff70bf773f to your computer and use it in GitHub Desktop.
Save technetium/549917077aa690a0d2f254ff70bf773f to your computer and use it in GitHub Desktop.
MySql functions to convert geocaching codes to id and back (Remove the GC, PR, and other prefixes)
CREATE FUNCTION GC_ID_TO_CODE(p_id INT)
RETURNS TEXT
DETERMINISTIC
RETURN IF (
p_id < 65536,
CONV(p_id, 10, 16),
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(CONV(p_id+411120, 10, 31),
'U', 'Z'), 'T', 'Y'), 'S', 'X'), 'R', 'W'), 'Q', 'V'), 'P', 'T'), 'O', 'R'), 'N', 'Q'), 'M', 'P'), 'L', 'N'), 'K', 'M'), 'J', 'K'), 'I', 'J')
)
CREATE FUNCTION GC_ID_TO_CODE(p_id INT)
RETURNS TEXT
DETERMINISTIC
RETURN IF (
p_id < 65536,
CONV(p_id, 10, 16),
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
CONV(p_id+411120, 10, 31),
'J', 'I'), 'K', 'J'), 'M', 'K'), 'N', 'L'), 'P', 'M'), 'Q', 'N'), 'R', 'O'), 'T', 'P'), 'V', 'Q'), 'W', 'R'), 'X', 'S'), 'Y', 'T'), 'Z', 'U')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment