Solved

SQL Update State Names with Abbreviation

Posted on 2012-04-12
7
623 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

22 Experts available now in Live!

Get 1:1 Help Now