Removing leading zeroes from a text field

K

kj96

Is there an Access function (or combination of functions) that can remove
leading zeroes from a text string?
 
F

fredg

Is there an Access function (or combination of functions) that can remove
leading zeroes from a text string?

Does the text string contain numbers after the leading zeros?
=Val("0005670")
returns 5670 as a number.
=Val("000ABC")
returns 0.
 
J

John Vinson

Is there an Access function (or combination of functions) that can remove
leading zeroes from a text string?

If it's numeric, use Val([fieldname]), or CStr(Val([fieldname])) to
get back to a string.

If it's text, or mixed numeric and text, after the zeros, you'll need
a little VBA function. Here's a recursive one off the top of my head,
test it first:

Public Function StripZeros(strIn As String) As String
If Left(strIn, 1) = "0" And Len(strIn) > 1 Then
StripZeros = StripZeros(Mid(strIn, 2))
Else
StripZeros = strIn
End If
End Sub



John W. Vinson[MVP]
 
J

John Vinson

John .. you did recursion ... eeewww!!! LOL!

A programmer who lived in Racine
Said "I can overload any machine -
My secret's aversion
To loops and recursion,
Just acres of inline routine!"


It's all right to write recursive code... just don't do it in public,
and wash your hands afterwards! <g>

John W. Vinson[MVP]
 
T

Tom Lake

Is there an Access function (or combination of functions) that can remove
If it's text, or mixed numeric and text, after the zeros, you'll need
a little VBA function. Here's a recursive one off the top of my head,
test it first:

Why not use LTrim?

Tom Lake
 
T

Tom Lake

Tom Lake said:
Why not use LTrim?

Tom Lake

OK, I know why. I thought of spaces while reading Zeros. Never mind!

LTrim(Str(Val(n)))

should work, though.

Tom L
 
J

John Vinson

OK, I know why. I thought of spaces while reading Zeros. Never mind!

LTrim(Str(Val(n)))

should work, though.

redundant since Val() already does the trimming... and it will fail
for input data such as "0000A32KW". The OP hasn't posted back with
more details so we may never know!

John W. Vinson[MVP]
 
T

Tom Lake

LTrim(Str(Val(n)))
redundant since Val() already does the trimming... and it will fail
for input data such as "0000A32KW". The OP hasn't posted back with
more details so we may never know!

Not redundant since the Str function puts a leading space for positive
numbers.

Tom L
 
Top