简体   繁体   中英

SQL how to cut off string

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

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

There are many more than just that, but I am wondering how either I could cut off the numbers in this column and just 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!

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()

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.

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.

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]) ?

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM