Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

SQL Update State Names with Abbreviation

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
ambientsbs
Asked:
ambientsbs
  • 2
  • 2
  • 2
  • +1
1 Solution
 
johanntagleCommented:
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
 
Anuradha GoliCommented:
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
 
johanntagleCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
ambientsbsAuthor Commented:
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
 
ralmadaCommented:
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
 
ambientsbsAuthor Commented:
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
 
ralmadaCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now