Solved

Microsoft SQL Query Zip Code Clean up

Posted on 2010-11-19
7
594 Views
Last Modified: 2012-05-10
I everyone,

I have a table that has a field for zipcodes, some of these zipcodes are 5 digit and others are the 9 digit, I would like to go and strip off everything except the first 5 digits, can someone help please?

thank you
0
Comment
Question by:dmanisit
7 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 34176439


Easy enough.  :)


update {mytable}
set zipcode = substr (zipcode, 1, 5)


Good Luck,
Kent
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34176501
its SUBSTRING

update mytable
set zipcode = substring(zipcode, 1, 5)
0
 
LVL 4

Expert Comment

by:Andre412
ID: 34176929
or left

UPDATE tbl
Set fld = Left(fld,5)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:8080_Diver
ID: 34176942
How about the following?

SQL1:
It is entirely possible to have leading spaces in VarChar fields

SQL2:
If you have stored the ZipCodes as Integers, then you have to convert them to VarChars, extract the left 5 characters, and then convert them back.

SQL1:

UPDATE yourtable

SET ZipCode = LEFT(LTRIM(ZipCode), 5);



SQL2:

UPDATE yourtable

SET ZipCode = LEFT(CONVERT(VarChar(9), ZipCode), 5);

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34176945
Hmm, lack of patience!  ;-)
0
 

Author Comment

by:dmanisit
ID: 34176962
Im sorry I may reopen the question just to award you some points.
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34191472
@dmanisit,

No worries, one way or the other. ;-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now