Mihawk
Mihawk

Reputation: 825

PL/SQL set exact length by random ora_hash

I'm useing ora_hash to get a random hash which Looks like this.

SELECT ORA_HASH (userID || SYSTIMESTAMP || SYS_GUID ()) AS hash 
FROM DUAL;

The userID is a nNumber with 5 digits. I've tested it a Long time and noticed that it generates me a number with a length between 7 and 10 digits.

Now I'd like to know if there is a good way to create a hash which always contains 10 Digits, means a number between 1000000000 and 9999999999.

Or should I take the differenct from the length to the number 10 and calculate it? What's the best way.

Upvotes: 0

Views: 962

Answers (2)

XING
XING

Reputation: 9886

Well you can anyways modify your query as well to get the 10 digit result. May be you can use as below:

select case when length(hash_val)> 10 then
           substr(hash_val,1,10)
           when length(hash_val) < 10 then
           lpad(hash_val,10,abs(dbms_random.random))
           else
           to_char(hash_val)
        end          
from (     
SELECT ORA_HASH ('00000' || SYSTIMESTAMP || SYS_GUID ()) AS hash_val 
FROM DUAL );

Upvotes: 1

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36987

SELECT 1000000000+ORA_HASH (userID || SYSTIMESTAMP || SYS_GUID ()) AS hash
FROM DUAL;

should do the trick (won't go up to 9999999999, though, since ora_hash only goes up to 4294967295, so +1000000000 makes it 5294967295 at most)

Upvotes: 0

Related Questions