如何通过获取某些字符前后的所有内容来创建列

How to create column by get everything after and before some characters

我在 SQL 服务器中有 table,如下所示:

select col from table

col1
------
02-567 City, ul. ABc 44, woj. Zak
56-123 City2, ul. Grt 78, woj. Maaap
44-153 Raw, Pl. 777, woj. Rapat

我需要创建查询,它会给我如下结果:

col1                                  col2   col3
----------------------------------------------------
02-567 City, ul. ABc 44, woj. Zak     City   ul. ABc 44
56-123 City2, ul. Grt 78, woj. Maaap  City2  ul. Grt 78
44-153 Raw, Pl. 777, woj. Rapat       Raw    Pl. 777

所以:

我在SQL服务器上怎么办?

select col1
, col2 = right(left(col1,charindex(',',col1)-1),charindex(',',col1)-1-charindex(' ',col1))
, col3 = ltrim(replace(reverse(parsename(replace(replace(reverse(col1),'.','•'),',','.'),2)),'•','.'))
from [table]

丑,我知道。

也许不那么难看 :),但我怀疑它的性能比 LukStorms 的答案低。

例子

Select  Col1
       ,Col2 =  stuff(Pos1,1,charindex(' ',Pos1+' '),'')
       ,Col3 =  Pos2
 From  YourTable A
 Cross Apply ( Select  Pos1 =  trim(JSON_VALUE(S,'$[0]'))
                      ,Pos2 =  trim(JSON_VALUE(S,'$[1]'))
                From  (values ( '["'+replace(string_escape([Col1],'json'),',','","')+'"]' ) ) B1(S) 
             ) B

结果

Col1                                   Col2     Col3
02-567 City, ul. ABc 44, woj. Zak      City     ul. ABc 44
56-123 City2, ul. Grt 78, woj. Maaap   City2    ul. Grt 78
44-153 Raw, Pl. 777, woj. Rapat        Raw      Pl. 777