Solved

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

Posted on 2004-08-31
8
512 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
  • 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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now