Discussion:
Removing leading zeros
(too old to reply)
Lee
2003-09-10 12:56:06 UTC
Permalink
I've received a data file from a client where all the
number fields have a leading zero at the left and zeros
following on the right. I've been able to get rid of the
following zeros by dividing each number by 100, but I
can't figure out how to get rid of the leading zero. I
know using the Right function would work but since these
fields contain dollar amounts the length is going to vary
for each record. Can someone suggest a way to get rid of
these zeros? Thanks-
Eric den Doop
2003-09-10 13:17:40 UTC
Permalink
Hello, Lee!
You wrote on Wed, 10 Sep 2003 05:56:06 -0700:

L> I've received a data file from a client where all the
L> number fields have a leading zero at the left and zeros
L> following on the right. I've been able to get rid of the
L> following zeros by dividing each number by 100, but I
L> can't figure out how to get rid of the leading zero. I
L> know using the Right function would work but since these
L> fields contain dollar amounts the length is going to vary
L> for each record. Can someone suggest a way to get rid of
L> these zeros? Thanks-

Try TRANSFORM(VAL("0001001")).
--
Eric den Doop
www.foxite.com - The Home Of The Visual FoxPro Experts - Powered By VFP8
Rick Bean
2003-09-10 13:20:23 UTC
Permalink
Lee,
If they are in a character field, the easiest would be to convert it to an integer and then convert it back to a string. e.g.
Assume one field is MyNum C(10), then to "fix" all of them:
REPLACE ALL MyNum with STR(VAL(MyNum), 10)

Rick
Post by Lee
I've received a data file from a client where all the
number fields have a leading zero at the left and zeros
following on the right. I've been able to get rid of the
following zeros by dividing each number by 100, but I
can't figure out how to get rid of the leading zero. I
know using the Right function would work but since these
fields contain dollar amounts the length is going to vary
for each record. Can someone suggest a way to get rid of
these zeros? Thanks-
Lee Mitchell
2003-09-10 13:39:02 UTC
Permalink
Hi Lee:

Can you use the STRTRAN() function as shown here to remove the leading zero?

x="0100"
?x
? STRTRAN(x,"0","",1,1)
? x


I hope this helps.

This posting is provided "AS IS" with no warranties, and confers no rights.

Sincerely,
Microsoft FoxPro Technical Support
Lee Mitchell

*-- VFP8 HAS ARRIVED!! --*
Read about all the new features of VFP8 here:
http://www.universalthread.com/VisualFoxPro/News/VFP8Release.asp
Purchase VFP8 here:
http://shop.microsoft.com/Referral/Productinfo.asp?siteID=11518

Keep an eye on the product lifecycle for Visual FoxPro here:
http://support.microsoft.com/default.aspx?id=fh;[ln];lifeprodv
- VFP5 Mainstream Support retires June 30th, 2003
- VFP6 Mainstream Support retires Sept. 30th, 2003
Post by Lee
I've received a data file from a client where all the
number fields have a leading zero at the left and zeros
following on the right. I've been able to get rid of the
following zeros by dividing each number by 100, but I
can't figure out how to get rid of the leading zero. I
know using the Right function would work but since these
fields contain dollar amounts the length is going to vary
for each record. Can someone suggest a way to get rid of
these zeros? Thanks-
George Nava
2003-09-10 14:36:18 UTC
Permalink
Three functions from my bag of tricks:

? TrimAll('00123400','0') && 1234
? TrimIni('XYXY1234','XY') && 1234
? TrimEnd('1234.266','6') && 1234.2

*--------------------------------------------------------
FUNCTION TrimIni(anyString, trimChar)
nChars = LEN(trimChar)
DO WHILE LEFT(anyString, nChars) == trimChar
anyString = SUBSTR(anyString, nChars+1)
ENDDO
RETURN anyString
ENDFUNC
*---------------------------------------------------------
FUNCTION TrimEnd(anyString, trimChar)
nChars = LEN(trimChar)
DO WHILE RIGHT(anyString, nChars) == trimChar
anyString = LEFT(anyString, LEN(anyString)-nChars)
ENDDO
RETURN anyString
ENDFUNC
*----------------------------------------------------------
FUNCTION TrimAll(anyString, trimChar)
nChars = LEN(trimChar)
DO WHILE LEFT(anyString, nChars) == trimChar
anyString = SUBSTR(anyString, nChars+1)
ENDDO
DO WHILE RIGHT(anyString, nChars) == trimChar
anyString = LEFT(anyString, LEN(anyString)-nChars)
ENDDO
RETURN anyString
ENDFUNC
*-----------------------------------------------------------

* WISH:
* Add trimChar to ALLTRIM, LTRIM and RTRIM functions
* Already posted in UT wishlist ;-)
Post by Lee
I've received a data file from a client where all the
number fields have a leading zero at the left and zeros
following on the right. I've been able to get rid of the
following zeros by dividing each number by 100, but I
can't figure out how to get rid of the leading zero. I
know using the Right function would work but since these
fields contain dollar amounts the length is going to vary
for each record. Can someone suggest a way to get rid of
these zeros? Thanks-
Lee
2003-09-10 14:50:41 UTC
Permalink
Thanks to everyone who replied. Using the suggestions I
received, I was able to solve the problem.
-----Original Message-----
I've received a data file from a client where all the
number fields have a leading zero at the left and zeros
following on the right. I've been able to get rid of the
following zeros by dividing each number by 100, but I
can't figure out how to get rid of the leading zero. I
know using the Right function would work but since these
fields contain dollar amounts the length is going to vary
for each record. Can someone suggest a way to get rid of
these zeros? Thanks-
.
Loading...