Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to Strip Leading Zero from Text String. 2

Status
Not open for further replies.

hefly

Technical User
Feb 6, 2008
134
0
0
US
I have a two digit number stored as text. Some numbers have a leading zero.

01
11
23
02


When there is a leading zero (number is stored as text), how can I use an access query to strip the zero from the text sring?

Thank you.

Hefly
 



hi,

Convert to a number CInt()

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
How do you do that in a query?

I am trying to link to another table where the number stored as text (without the leading zero).
 
You can use SQL view, it is not possible to create such a query in design view:

[tt]SELECT * FROM test1
INNER JOIN test2
ON Val(test1.ID)=Val(test2.ID)[/tt]

 
Ooops.

I get a "data type mismatch" test1.id is text and test2.id is numerical.

Any other suggestions?

Thanks.

Hefly
 
The problem has nothing to do with the fact that one field is numeric and the other is text. The problem is that you have null values in one of those fields.
val(Null) throws an error.

Val(NZ(test1.ID,0))=Val(NZ(test2.ID,0))
 
You can use CInt, as suggested by SkipVought, it will also deal with Nulls, you need it on both sides, unless you know that the numeric side does not have any nulls.

[tt]SELECT * FROM test1
INNER JOIN test2
ON CInt(test1.ID)=CInt(test2.ID)[/tt]

 
Remou:

Thank you very much. I tried the solution using CInt() but I get another error: "Invalid use of Null."

Hefly
 
My solution as posted works. Not sure what Remou is thinking, but he is wrong about Cint converting a null. From the helpfile to show this
Each function coerces an expression to a specific data type.

Syntax
CBool(expression)
CByte(expression)
CCur(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
CLng(expression)
CSng(expression)
CStr(expression)
CVar(expression)
The required expression argument is any string expression or numeric expression.

CINT fails on a Null.
 
I try the following query and I get "data type mismatch."

Code:
SELECT test1.Section, test2.Sec
FROM test1 INNER JOIN test2 ON test1.Section = test2.Sec
WHERE ((CInt(test1.Section) Is Not Null) AND (CInt(test2.Sec) Is Not Null));

And the following expression returns the error "Invalid use of Null"

Code:
SELECT test1.Section, test2.Sec
FROM test1 INNER JOIN CInt(test2 ON test1.Section) = CInt(test2.Sec)
WHERE ((CInt(test1.Section) Is Not Null) AND (CInt(test2.Sec) Is Not Null));
 
What do I have to do? I already explained this in the post, that is going to fail. Why will you not use the solution I provided? It works as I have stated.
val(NZ(test1.ID,0))=Val(NZ(test2.ID,0))
or
CINT(NZ(test1.ID,0))= CINT(NZ(test2.ID,0))

Your other solution is to first build a query/ies that removes the nulls and then join them.
 
Skip & Remou:

Thanks for your help... I found something that worked!

Code:
SELECT CInt(test1.Section) AS Expr1, CInt(test2.sec) AS Expr2
FROM test1 INNER JOIN test2 ON CInt(test1.Section)=CInt(test2.sec)
WHERE (((test1.Section) Is Not Null));
 
MajP

Thanks for providing a solution...and an explanation. I got it now. I applied your solution improperly at first and got an error.


Hefly
 
Why not simply this ?
Code:
SELECT test1.Section, test2.sec
FROM test1 INNER JOIN test2 ON Val(test1.Section)=Val(test2.sec)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top