Home > database >  How to extract only numbers from a python generated hash
How to extract only numbers from a python generated hash

Time:06-03

So my idea was to create a hash from dog's info for a database. But after trying to input the hash into the DB it didn't work because of data types incompatibility. So I wanted to get only numbers and drop all the letters from a hash, if possible.

import hashlib
DogName = 'Dog'
DogID = '1'
Hash1 = DogName   DogID
Hash2 = Hash1.encode('utf-8')
out = hashlib.sha1(Hash2).hexdigest()
print(out)

And I get a hash 9ef8ed01647767afb5e0c4c45b4c373e73f795c1

So is this possible to get numbers only?

I found something similar, but this didn't work for me.

CodePudding user response:

CREATE TABLE test (
    hash CHAR(40), 
    digits_from_hash VARCHAR(40) AS (REGEXP_REPLACE(hash, '[^0-9]', ''))
);
INSERT INTO test (hash) VALUES ('9ef8ed01647767afb5e0c4c45b4c373e73f795c1');
SELECT * FROM test;
hash digits_from_hash
9ef8ed01647767afb5e0c4c45b4c373e73f795c1 9801647767504454373737951

db<>fiddle here

  • Related