views:

90

answers:

6

I have column of strings with a ctiy state and number in each.

SPOKANE, WA           232/107
LAS VEGAS, NV         232/117
PORTLAND, OR          232/128

Theres many more than just that but I am wondering how either I could cut off the numbers in this column and jsut show the city and state or even better cut off the numbers and make city and state a separate column.

The column is in the same format all the way down for all the different records.

Thanks!

+6  A: 

Without doing all of the work for you...

City: A substring of the column from position 0, to the first occurence of a comma - 1.
State: A substring of the column from 2 positions after the first occurence of a comma, to the next position that is a space... trimmed.

see: SUBSTRING(), CHARINDEX(), PATINDEX()

Fosco
This is what I was looking for...thank you
[PATINDEX](http://msdn.microsoft.com/en-us/library/ms188395.aspx) gets no love :/
OMG Ponies
@OMG Ponies, lol.. added.
Fosco
A: 

To remove the numbers at end use the substring method, something like this.

@str = SUBSTRING(@str, LEN(@str)-7, 7)

To separate the city and state you'll need some kind of split function but I can't remember the syntax off top of my head sorry.

Ash Burlaczenko
A: 

EDIT Whoops. Just saw that the question was how, not if you should. That's what I get, I guess.

Other answers here are looking at substring(), so I'll leave that part of your question alone.

As far as splitting them into a different column: If the xxx/yyy does not have any direct correlation to City and State, then I would definately move those to a different column. Indeed, I would have at least three columns, here: City, State, [WhateverYouCallThatCode]

Continued EDIT If they'll always be at the right hand of your column, how about Right(7, [Column])?

AllenG
+1  A: 

I already figured it out and had written the SQL... then I saw Fosco's answer, but since I have it I might as well post it anyway:

SELECT
    LEFT(yourcolumn, CHARINDEX(',', yourcolumn) - 1) AS City,
    RIGHT(LEFT(yourcolumn, CHARINDEX(',', yourcolumn) + 3), 2) AS State
FROM yourtable

The difference between this algorithm and Fosco's is that this assumes that the state is exactly 2 letters. If that is not always true then you should use the other answer.

Mark Byers
+1 Assuming 2 character state is a given, I like this a lot better.
Fosco
A: 

I think this will break your field down in all the ways suggested so far...

SELECT
   substring([field], 1, patindex('%[1-9]%', [field])-1) as [CITY_AND_STATE],
   substring([field], 1, charindex(',', [field])-1) as [CITY_ONLY],
   substring([field], charindex(',', [field]), patindex('%[1-9]%', [field])-1)) as [STATE_ONLY],
   substring([field], patindex('%[1-9]%', [field]), len([field])) as [NUMBERS_ONLY]
FROM
   [table]
dave
A: 

Consider using the REVERSE function. If you reverse the string, then the position of the first space in the reversed string can be subtracted from the length of the normal string to calculate the starting position of NNN/MMM.

The method has worked well for me for parsing file paths. Reversing the string and looking for the first "\" in the reversed string indicates where the path ends and the file name begins.

If your data is as well formatted as you indicate, this method may be more trouble than some others. But when the beginning data is "haphazard", this method can really simplify things.

Darryl Peterson