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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling Minimum Lengths 3

Swi

Programmer
Feb 4, 2002
1,978
US
Hi,

I have the below query. My thought was the part at the end would only select records that had a +4 zip code (12345-1234), however this does not seem to work. Any ideas?

Thanks.

Code:
"SELECT Min(Len([NAME])) AS [NAME], " & _
            "Min(Len([ADDR2])) As [ADDR2], Min(Len([ADDR1])) As [ADDR1], " & _
            "Min(Len([CITY ST ZIP])) As [CITY ST ZIP], Min(Len([Salutation])) As [Salutation] " & _
            "FROM [Test.csv] WHERE LEFT(RIGHT([CITY ST ZIP],5),1) = '-'"
 
You never said why you think the expression didn’t seem to work or provide examples.

Most of us recommend three separate fields for City, State, and Zip. Maybe you have no control over this.
 
I will give that a shot. After I posted I think I may have found the issue but had to leave for the day. I will check it Monday and report back. Thanks.
 
I think you have trailing spaces in the field, so the thought of having a - in the left 1 char of the rightmost 5 chars is wrong, you first need to trim off spaces (and other whitespace, if there may be tabs, for example, even if only accidentally).

Gammastros LIKE expression (pay close attention to the single - outside square bracket expresions for digits) will give you an even better filter of only rows that conform to ddddd-dddd when d stands for a digit. The % allow any further characters before and after. which is crucial, as you likely have trailing spaces. You could avoid them when you'd make it a varchar field and trim before you store an address, I still think there would be other ZIPs that could have letters in international addresses, you'll just not be interested in them.

To follow up on the advice to store city, street and ZIP in three fields, if you know they're in that order you could use the SPLIT() function, but this is also not straight forward, as all of the three parts can have spaces in them with international addresses, so you likely get more than 3 parts from a split and have to decide which parts to combine. It's even less viable with international addresses that can vary from this pattern overall.
 
Last edited:
Hi,

Agree about the split fields however this is the output data format that is needed for the machinery it is running on so I am stuck with this format. I did get this working earlier today. It was spacing so I just added the TRIM and all was good. Thanks all.
 
Glad you got that going. Still a normalized data can feed anything that needs the address composed, you can easily write a query putting the single values together after tidying up the data.
 

Part and Inventory Search

Sponsor

Back
Top