Skip to content

Instantly share code, notes, and snippets.

@slava-konashkov
Last active March 30, 2023 10:52
Show Gist options
  • Save slava-konashkov/f20edbd504febd4e578ff877b2778761 to your computer and use it in GitHub Desktop.
Save slava-konashkov/f20edbd504febd4e578ff877b2778761 to your computer and use it in GitHub Desktop.
Anonymizing and masking sensitive data in SQL
-- Random Int
SELECT FLOOR(RAND() * 100);
-- Random float
select FLOOR(RAND() * 1000 ) + round(rand() * 0.49 + 0.01, 2);
-- Random Srting
SELECT MD5(RAND());
SELECT TO_BASE64(UNHEX(HEX(MD5(RAND()))));
-- Random Srting with random length
SELECT SUBSTR(MD5(RAND()), 1, FLOOR(RAND() * 12) + 1);
SELECT SUBSTRING(TO_BASE64(UNHEX(HEX(MD5(RAND())))) FROM 1 FOR FLOOR(RAND() * 10) + 1);
-- Random char
SELECT substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand()*26+1, 1);
-- get domain from email
SELECT SUBSTRING_INDEX(email, '@', -1);
-- random email
SELECT CONCAT(SUBSTR(MD5(RAND()), 1, FLOOR(RAND() * 12) + 1), '@', SUBSTRING_INDEX(email, '@', -1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment