extracting zip from city, state zip

deekras

Registered User.
Local time
Today, 14:47
Joined
Jun 14, 2000
Messages
169
i am getting info from a table that has city, state zip as 1 field. i only need the zip info. some zips have 5 and others have 5+4 digits. the info has spaces between the city, state and zip (Beverely Hills, CA 90210) so i can not use the instr method.

can i do something that will just give me the first 5 digits?
 
if the 5+4 zips are in the proper format nnnnn-nnnn, you could read the string backwards looking for the first space character. then extract a substring of 5 characters, giving you the 5 digit zip...

there is also an example in NeatCode 97 that will probably work for you...

hth,
al




[This message has been edited by pcs (edited 07-25-2001).]
 
thanks for your quick response.

can you please write the coding for that. i'm new at this. thanks so much!
 
you should get a copy of neatcode, it's available from the MS website. it is an mdb with lots of handy stuff. if you can't find it, let me know and i will try to find the URL.


here is the code from neatcode97:

------------------------------

Sub ParseCSZ(ByVal S As String, City As String, State As String, Zip As String)
'
' Parses address "New York NY 00123" into separate fields.
' Supports the following formats:
' New York NY 12345-9876
' Pierre, North Dakota 45678-7654
' San Diego, CA, 98765-4321
'
' Words are extracted in the following order if no commas are found to delimit the values:
' Zip, State, City
'
Dim P As Integer
'
' Check for comma after city name
'
P = InStr(S, ",")
If P > 0 Then
City = Trim$(left$(S, P - 1))
S = Trim$(Mid$(S, P + 1))
'
' Check for comma after state
'
P = InStr(S, ",")
If P > 0 Then
State = Trim$(left$(S, P - 1))
Zip = Trim$(Mid$(S, P + 1))
Else ' No comma between state and zip
Zip = CutLastWord(S, S)
State = S
End If
Else ' No commas between city, state, or zip
Zip = CutLastWord(S, S)
State = CutLastWord(S, S)
City = S
End If
'
' Clean up any dangling commas
'
If Right$(State, 1) = "," Then
State = left$(State, Len(State) - 1)
End If
If Right$(City, 1) = "," Then
City = left$(City, Len(City) - 1)
End If
End Sub

----------------------------------------------------------------

think this will probably suit your needs...you may need to do a left() on the string zip
if you only want 5 characters...

hth,
al
 
wow! i thought it could be done much simpler. would this work in a query? i need to send the query to excel to be processed from there?
 
i tried this in the query:

first field
step1:right([csz],(instr([csz],",")+1)

next field:
step2:mid([step1],instr([step1]," ")+4)

third field
zip:left([step2],5)

this gets me the info i am looking for. thanks for your inspiration.
 
sorry i mis-lead you a bit...but, it's your fault for posting your question to the correct forum
smile.gif

that's rare, indeed...

(i wasn't aware that you were exporting to excel...)

glad you got a solution. and, best of all, you figured it out on your own!!!

al


[This message has been edited by pcs (edited 07-25-2001).]
 

Users who are viewing this thread

Back
Top Bottom