Solved

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

Posted on 2004-08-31
8
541 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 200 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 50 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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