Remove leading zero from partial string

ExcelPowerSq

New Member
Joined
Apr 13, 2015
Messages
7
Hi,
I have a complicated string where I need to remove leading zero from partial string and change the string with different formatting. Example below:

OriginalNew
02-01-22 [G MM]2/1/22
3-05-01 [X SM]3/5/1
07-6-10 [E SM]7/6/10

<tbody>
</tbody>

I have tried using NumberValue but that returns an integer value. I have tried TEXT with mm/dd/yy but with that data is all incorrect due to there being no 0 days or 0 months.

I can replace the '-' with '/' easily by using substitute function but that still does not solve my original problem of removing leading zero.

Wish there was regex support in Excel. Your help is much appreciated!

Thanks,
K
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
With your sample data in Col_A
Try this approach:
Code:
B2: =SUBSTITUTE(TEXT(--(LEFT(A2,FIND(" ",A2))),"m/d/y"),"/0","/")

Is that something you can work with?
 
Last edited:
Upvote 0
With your sample data in Col_A
Try this approach:
Code:
B2: =SUBSTITUTE(TEXT(--(LEFT(A2,FIND(" ",A2))),"m/d/y"),"/0","/")

Is that something you can work with?
Will that work correctly in locales that use the reverse day-month order from your locale? In case not, here is an alternative...

=SUBSTITUTE(MID(SUBSTITUTE("-"&LEFT(A1,FIND(" ",A1)-1),"-0","-"),2,8),"-","/")
 
Last edited:
Upvote 0
Unless I'm missing something...It seems that if the m/d order is reversed from the US convention, then this would work:
Code:
B2: =SUBSTITUTE(TEXT(--(LEFT(A2,FIND(" ",A2))),"d/m/y"),"/0","/")
 
Upvote 0
Unless I'm missing something...It seems that if the m/d order is reversed from the US convention, then this would work:
Code:
B2: =SUBSTITUTE(TEXT(--(LEFT(A2,FIND(" ",A2))),"d/m/y"),"/0","/")
I am under the impression that if, say, the value in front of the space is, say, 6/1/20, then in the US this part of the formula...

TEXT(value,"d/m/y")

would yield June 1, 2020 whereas in England it would yield January 6, 2020 so that your "d/m/y" would produce different results. Am I not correct in that impression? I have absolutely no experience with international date issues, so I do not know for sure myself, but that is what I gathered from other discussion about dates in the past.
 
Last edited:
Upvote 0
With formula, =SUBSTITUTE(TEXT(--(LEFT(A2,FIND(" ",A2))),"M/D/y"),"/0","/")

Following values yield #VALUE ! error.

2-0-0 [XE SM]#VALUE !
0-0-02 [GE SM]#VALUE !
0-0-03 [GE SM]#VALUE !

<tbody>
</tbody>


It appears that as long as the Day is 0 value, formula will not be able to process data.
 
Upvote 0
Will that work correctly in locales that use the reverse day-month order from your locale? In case not, here is an alternative...

=SUBSTITUTE(MID(SUBSTITUTE("-"&LEFT(A1,FIND(" ",A1)-1),"-0","-"),2,8),"-","/")


=SUBSTITUTE(MID(SUBSTITUTE("-"&LEFT(A1,FIND(" ",A1)-1),"-0","-"),2,8),"-","/") seems to trim single digit 0s

2-0-0 [XE SM]2//
0-0-01 [GE SM]//1
7-0-00 [GE SM7//0

<tbody>
</tbody>


Any suggestions on how to include single 0s as well in your formula? By the way these are not date field. These are supposed to be ports on a network device.

Thanks
 
Upvote 0
=SUBSTITUTE(MID(SUBSTITUTE("-"&LEFT(A1,FIND(" ",A1)-1),"-0","-"),2,8),"-","/") seems to trim single digit 0s

2-0-0 [XE SM]2//
0-0-01 [GE SM]//1
7-0-00 [GE SM7//0

<tbody>
</tbody>


Any suggestions on how to include single 0s as well in your formula? By the way these are not date field. These are supposed to be ports on a network device.
The formula you quoted was developed to reverse the month and day fields for dates... there are no single zero days or months so my formula did not have to worry about them. Instead of grabbing a formula meant for reversing date parts and wondering why it doesn't work for non-dates, why don't you tell us what your specific problem is... what does your original values look like, what should they look like after the formula processes them (showing us several specific examples of these two situations would be most useful).
 
Upvote 0
The formula you quoted was developed to reverse the month and day fields for dates... there are no single zero days or months so my formula did not have to worry about them. Instead of grabbing a formula meant for reversing date parts and wondering why it doesn't work for non-dates, why don't you tell us what your specific problem is... what does your original values look like, what should they look like after the formula processes them (showing us several specific examples of these two situations would be most useful).

I will gladly present what I need.

I have bunch of ports which are formatted in a special way so they can be sorted properly in Excel and them bulk imported into an application. The problem is Excel-formatted ports are not what application will recognize.

Some examples below:


Excel-Formatted data (current)Requested data (new format)
2-0-0 [XE SM]2/0/0
0-0-01 [XE SM]0/0/1
0-0-02 [XE SM]0/0/2
0-0-03 [GE SM]0/0/0
7-1-00 [GE SM]7/1/0
7-1-01 [XE SM]7/1/1
2-1-10 [XE SM2/1/10
2-0-00 [GE SM]2/0/0
7-0-16 [GE SM]7/0/16
7-0-02 [GE SM7/0/2
02-01-21 [GE MM]2/1/21

<tbody>
</tbody>

As you can see from above example x/y/z, x and y will always be single-digit, whereas z can be double-digit or single-digit. Neither of the x,y or z can have a leading zero.

Hope that helps.

Thanks
 
Upvote 0
As you can see from above example x/y/z, x and y will always be single-digit, whereas z can be double-digit or single-digit. Neither of the x,y or z can have a leading zero.

Give this formula a try...

=0+LEFT(A1,FIND("-",A1)-1)&"/"&0+SUBSTITUTE(MID(A1,FIND("-",A1)+1,2),"-","")&"/"&0+MID(A1,FIND("-",A1,FIND("-",A1)+1)+1,2)
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top