Solved

SQL Update State Names with Abbreviation

Posted on 2012-04-12
7
658 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 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