Solved

SQL Update State Names with Abbreviation

Posted on 2012-04-12
7
719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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