I would like to add zeros to an alphanumeric string if it's one digit.
What I've got is something like :
CV-1-1A
CV-1-32
CV-12-24
CV-1-2
CV-1-2A
and I would like to change it to:
CV-01-01A
CV-01-32
CV-12-24
CV-01-02
CV-01-02A
I would like to add zeros to an alphanumeric string if it's one digit.
What I've got is something like :
CV-1-1A
CV-1-32
CV-12-24
CV-1-2
CV-1-2A
and I would like to change it to:
CV-01-01A
CV-01-32
CV-12-24
CV-01-02
CV-01-02A
Copyright © 2021 Jogjafile Inc.
LPADcan be used to zero-pad (or pad with any character, for that matter) a string. However, it can't work inside a string, so some trickery is required. Assuming all your values have the format ofa-b-cwherea,bandcare strings up to 3 characters, you could useSUBSTRING_INDEXto break the string up into parts,LPADeach part individually, and useCONCATto put them all back together:Here's an SQLFiddle with a working example.