I just started learning SQL and there is my problem.
I have a column that contains acronyms like "GP2", "MU1", "FR10", …. and I want to add ‘0’s to the acronyms that don’t have enough characters.
For example I want acronyms like "FR10", "GP48",… to stay like this but acronyms like "MU3" must be converted into "MU03" to be as the same size as the others.
I already heard about LPAD and RPAD but it just add the wanted character at the left or the right.
Thanks !
3
Answers
Thank you all for your response. I think i did something similar as Isolated. Here is what I've done ("acronym" is the name of the column and "destination" is the name of the table) :
Thanks !
Is the minimum length 3 as in your examples and the padded value should always be in the 3rd position? If so, use a
case expression
andconcat
such as this:A
regexp_replace()
:D
at the start of the string^
d
at the end$
{0,4}
()
with a backreference1
and2
.repeat()
up to the total length of 4.It’s good to consider additional test cases.