0

I want to automatically generate an unique entity field in Postgresql in following format:

"ССDDD" where "C" is a character and "D" is a digit.

For example "FG1752" or "HK9273". How can this be implemented?

1
  • Why do you want to do this? Is it just to produce a non obvious numeric identifier? If so you could use something like hashids to accomplish this more easily Commented May 25, 2021 at 18:47

1 Answer 1

1

here is one way in mysql :

SELECT concat(char(floor(rand() *(90-65+1)+65)), 
       char(floor(rand() *(90-65+1)+65)), 
       lpad(floor(rand() *(10000)),4,0) 
) RandomAlphanumeric

in postgresql:

SELECT concat(chr(floor(random() *(90-65+1)+65)::int), 
       chr(floor(random() *(90-65+1)+65)::int), 
       lpad(floor(random() *(10000))::varchar(4),4,'0') 
) RandomAlphanumeric

I'm producing a random number between 65 and 90 which are ascii code for A to Z

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.