Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Split City, State ZIP into three columns from one in SELECT statement

Posted on 2004-08-31
8
Medium Priority
?
597 Views
Last Modified: 2008-03-10
I have a column with the city, state and zipcode.  I need to split this column into three within a select statement.
0
Comment
Question by:lexo
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Assisted Solution

by:SjoerdVerweij
SjoerdVerweij earned 800 total points
ID: 11947900
declare @t varchar(50)

set @t = 'Sacramento, CA 95999'

select
  city = left(@t, charindex(',', @t) - 1),
  state = rtrim(ltrim(substring(@t, charindex(',', @t) + 1, 3))),
  zip = right(@t, 5)

Replace @t with your column name to use the expressions in a query.
0
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 200 total points
ID: 11947949
As long as its formatted correctly (consistantly),

declare  @temp1 varchar(255)

set @temp1 = 'city, state 99999-5555'
--set @temp1 = 'city, state 99999'

select left(@temp1,charindex(',',@temp1)-1) as city,
         substring(@temp1, charindex(',',@temp1) + 2,charindex(' ',@temp1,charindex(',',@temp1))) as state,
         substring(@temp1,charindex(' ',@temp1,charindex(',',@temp1))+ charindex(',',@temp1)+2,len(@temp1)-(charindex(' ',@temp1,charindex(',',@temp1))+ charindex(',',@temp1))) as zip
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 11947953
The code I posted will work if state is an abbrev or full and if your zips are 5 or 5-4.
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 26

Expert Comment

by:Hilaire
ID: 11949984
If the above suggestions don't provide a working answer,
please let us know and post sample data
0
 

Author Comment

by:lexo
ID: 11953412
Why am I getting this error?

'varchar' is not a recognized CURSOR option.
0
 
LVL 18

Expert Comment

by:SjoerdVerweij
ID: 11953982
Post your full query.
0
 

Author Comment

by:lexo
ID: 11954270
nevermind, I was missing an "@"
Thank you for your help!!
0
 

Author Comment

by:lexo
ID: 11954313
I split the Points, giving SjoerdVerweij the majority because he posted first, and the code works well.  However, ChrisFretwell's code also works, and it has additional functionality.
Again, thank you all!!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

715 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