SQL如何将一个列中值内的逗号分割成另一列

2023-03-03 20:09:46 浏览数 (3)

有时候,我们会想将一个列中的值分成多列。

示例

例如某个列是这样的: 7890 – 20th Ave E Apt 2A, Seattle, VA 9012 W Capital Way, Tacoma, CA 5678 Old Redmond Rd, Fletcher, OK 3456 Coventry House Miner Rd, Richmond, TX

代码

1. MS SQL Tips

代码语言:javascript复制
SELECT 
     REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 1)) AS [Street]
   , REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 2)) AS [City]
   , REVERSE(PARSENAME(REPLACE(REVERSE(myAddress), ',', '.'), 3)) AS [State]
FROM dbo.custAddress

结果:

Street

City

State

7890 – 20th Ave E Apt 2A

Seattle

VA

9012 W Capital Way

Tacoma

1 人点赞