SQL how to cut off string

为君一笑 提交于 2021-02-07 08:25:37

问题


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!


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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]



回答6:


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.



来源:https://stackoverflow.com/questions/3374763/sql-how-to-cut-off-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!