Insert Leading Zeros

H

hkslater

I have a data field that I want to instert a leading zero (where necessary)
so that I may sort the data correctly. Is there an easy way to do this?

My service codes currently sort as follows:

32-1
32-10
32-11
32-12
32-2
32-20
32-21
32-3

Is there formula that will insert a leading zero into the 32-1, 32-2, 32-3
etc. to make it 32-01, 32-02, 32-03?

Thanks for the help.
 
F

Frank Kabel

Hi
you may try the following in a helper column:
=LEFT(A1,FIND("-",A1) & TEXT(--(MID(A1,FIND("-",A1),10)),"00")
 
F

Frank Kabel

Hi
or another alternative (a little bit shorter):
=TEXT(--SUBSTITUTE(A1,"-",""),"00-00")
 
T

Tim C

Assuming no more than 2 digits to the right of the dash, try:

=LEFT(A1,FIND("-",A1))&TEXT(RIGHT(A1,2),"00;00")

Tim C
 
N

N Harkawat

Assuming that their are only 2 characters left of "-"
=IF(LEN(MID(A1,3,1024))=2,SUBSTITUTE(A1,"-","-0"),A1)
 
M

Myrna Larson

Hi, Frank:

You are staying up too late <bg>!

Your first formula was missing a left paren before the first &. After that
fix, it gave me two dashes in the results, which was fixed by inserting +1
before the ",10" in the MID formula. So it ended up as this, which worked
correctly.

=LEFT(A1,FIND("-",A1)) & TEXT(--(MID(A1,FIND("-",A1)+1,10)),"00")

For the 2nd, shorter formula, no cigar on that one <bg>. It inserts the
additional zero at the front instead of after the dash.

32-1 comes out as 03-21 instead of 32-01

Myrna Larson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top