Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pad single digit numbers in column with leading zero

I have a requirement to pad any single digit numbers in a string field with a leading zero. I only need to pad single numeric characters with a leading zero, not string alphabetic characters (A-Z). Below is an example of the current data, along with the expected output for these examples.

Current output     Expected output:
9                  09    
19                 19   
15                 15
F                  F
UR                 UR           
B                  B  
0                  00    
4                  04
N                  N
00                 00

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use a CASE expression to check whether the column contains only 0 to 9. If true then prefix with 0 else as it is.

Query

select 
  case when col like '[0-9]' 
    then '0' + col
  else col end as newcol  
from tablename;     
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading