0

I'm looking for an hashing function which is implemented in both PostgreSQL and MySQL, which can hash a string and return an integer.

For example I tried to use hashtext function in PostgreSQL, but I haven't found the same function implementation in MySQL.

I've seen that MySQL supports md5, but it returns a string instead of an integer.

5
  • 1
    how can a md5 return a integer? Commented Mar 30, 2022 at 22:20
  • md5 does return a string, but in my usecase I would need an integer returned. For example, hashtext() function in postgresql does return an integer Commented Mar 30, 2022 at 22:24
  • This would go a lot better if you provided some example data and a rationale for what you are doing. Add answers as update to question. Commented Mar 30, 2022 at 22:33
  • i doubt that an integer will not have many collisions, you can ways program doxygen.postgresql.org/… as function or procedure to generatw Commented Mar 30, 2022 at 22:38
  • Can you please edit your question to post a link to documentation / explanation for the hashtext() function you mention? Commented Mar 30, 2022 at 22:43

1 Answer 1

3

The Postgres query

SELECT encode(sha224('foo'::bytea), 'hex')

and the MySQL query

SELECT SHA2('foo', 224)

both return the same number encoded as a hex string, 0808f64e60d58979fcb676c96ec938270dea42445aeefcd3a4e6f8db.

MySQL has the CRC32() function to return an unsigned 32-bit cyclic redundancy check number on a a string. You can create the same capability with a stored function in postgres. For example: CRC32 function with PL/pgSQL

They get the same value. Postgres fiddle MySql fiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.