Solved

SQL Update State Names with Abbreviation

Posted on 2012-04-12
7
646 Views
Last Modified: 2012-04-13
Hi Experts,

I've searched up and down for the right way to do this and can't figure it out. I have a table with records that look like such:

1    John    Doe      Houston     Texas  
2    Adam  Dean    Reno           Nevada
3    Mark   Mill       Medford     Oregon


I want to write a query that will look through my records and return the state abbreviation where the state name currently is. The query could either hard code this information or reference my "State_Abbreviations" table which looks like this:


1    Alabama    AL
2    Alaska        AK
3    Arizona      AZ
4    Arkansas   AR


All in all, I would like my final data to look like this:


1    John    Doe      Houston     TX  
2    Adam  Dean    Reno           NV
3    Mark   Mill       Medford     OR



Please help with the best way to accomplish this! Thank you!
0
Comment
Question by:ambientsbs
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 37841452
select a.first_name, a.last_name, a.city, s.short_state_name
from table_a a join State_Abbreviations s
on a.city=s.city
0
 
LVL 12

Expert Comment

by:Anuradha Goli
ID: 37841476
you can use either of scripts to achieve your result.
SELECT	S.FIRSTNAME,
		S.LASTNAME,
		S.CITY,
		SA.ABBREVSTATENAME
FROM	DATATABLE S 
INNER 
JOIN	STATE_ABBREVIATIONS SA
ON		S.CITY = SA.CITY
--------------------------------------------------------------------------
SELECT	S.FIRSTNAME,
		S.LASTNAME,
		S.CITY,
		(	
			SELECT	ABBREVSTATENAME 
			FROM	STATE_ABBREVIATIONS 
			WHERE	CITY = S.CITY
		) AS ABBREVSTATE
FROM	DATATABLE S

Open in new window

0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37841481
Sorry, the my query should have been:

select a.first_name, a.last_name, a.city, s.short_state_name
from table_a a join State_Abbreviations s
on a.long_state_name=s.long_state_name
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:ambientsbs
ID: 37843291
Ok, so would this just SELECT the data in my table? I'd like the script to UPDATE each record if that's possible. Thanks for your help!
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 37844025
something like

update a
set a.state = b.abbreviated_name
from yourtable a
inner join State_Abbreviations b on a.state = b.long_name

Open in new window

0
 

Author Closing Comment

by:ambientsbs
ID: 37844977
This led me in the right direction. I'm using MySQL so not sure if that made a difference but here' s what worked for me:

UPDATE mytable S
INNER JOIN State_Abbreviations SA
ON S.State = SA.State_Name
SET S.State = SA.Abbreviation


Thanks for the help!
0
 
LVL 41

Expert Comment

by:ralmada
ID: 37845239
yep, sorry, I've thought you were on MS SQL server. MySQL syntax is different. I'm glad you were able to make it work.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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