I currently have functions in Postgres and Redshift that take a randomly generated string, hash it, then uses part of the hash to generate a random number between 0-99. I am trying to replicate this functionality in Azure SQL Data Warehouse such that I get the same value in SQL DW as I do in Postgres and Redshift.
The issue I'm running into is when I cast the result to a VARCHAR or use a string function the result is a much different string. I'd like to get the result of the md5 function as an identical VARCHAR
.
To illustrate, here is a query in Azure SQL DW:
SELECT
'abc123' as random_string,
HASHBYTES('md5', 'abc123') as md5,
CAST(HASHBYTES('md5', 'abc123') AS VARCHAR) as md5_varchar,
RIGHT(HASHBYTES('md5', 'abc123'), 5) as md5_right
;
This yields
random_string,md5,md5_varchar
abc123,0xE99A18C428CB38D5F260853678922E03,éšÄ(Ë8Õò`…6x’.,6x’.
As you can see, the resulting varchar is much different from the output of the md5 function. Is there a way to convert the result of md5 into an identical string?
In Postgres and Redshift the result of the md5
function is a VARCHAR so it is simple to do transformations on it.
Here are the queries in Redshift and Postgres:
-- Redshift
SELECT
'abc123' as random_string,
right(strtol(right(md5('abc123'), 3), 16), 2)::INT as tranche
;
-- Postgres
SELECT
'abc123' as random_string,
right(('x' || lpad(right(md5('abc123'), 3), 4, '0')) :: BIT(16) :: INT :: VARCHAR, 2) :: INT AS tranche
;
Both functions return the value 87
.