根据一些标准将句子分开

时间:2019-08-02 04:08:28

标签: sql sql-server

我有数据库FSTP。该数据库包括2个表。 dbo.CLIENTSdbo.CLIENTS_address

我应该从dbo.CLIENTS中选择[address]列,如下所示:

  1. 没有电话号码:
    010010,c.Paris,Saint-Gilles,st。街b。 75003 ,。 27

  2. 有电话号码:
    邮编:14097,水牛城,Capen-Hall,NY 14260。 1611.电话:(716)645-3321

我想将此文本分为几列并写入dbo.CLIENTS_address

我写代码。与邮编和城市搭配很好。
但是我不能分开地址和电话

select
 case when CHARINDEX(' ',CLIENTS.adres)>0 then
     SUBSTRING(CLIENTS.adres,1,CHARINDEX(' ',CLIENTS.adres)-2) 
 else 
     adres 
 end [ZIP],
 case when CHARINDEX(',',CLIENTS.adres)>0 then 
     SUBSTRING(CLIENTS.adres,9,CHARINDEX(',',CLIENTS.adres,9)-9) 
 else 
     adres 
 end [city], 
 CASE WHEN CHARINDEX(', ',CLIENTS.adres)>0 THEN 
 SUBSTRING(CLIENTS.adres,CHARINDEX(',',CLIENTS.adres,9)+1,len(CLIENTS.adres))               
 ELSE 
    NULL 
 END as [Address],
 CASE WHEN CHARINDEX('Phone',CLIENTS.adres)>0 THEN
     SUBSTRING(CLIENTS.adres,CHARINDEX('Phone. ', CLIENTS.adres)+5,len(CLIENTS.adres)) 
 ELSE 
     NULL 
 END as [Phone]
  from [FSTP].[dbo].[CLIENTS]

我期望的结果是:

 SELECT * FROM CLIENTS_address;
+-------+-----------+-----------------------------------------+--------------+
| zip   | city      | Address                                 | Phone        |
+-------+-----------+-----------------------------------------+--------------+
| 010010| Paris     | Saint-Gilles,  st. Rue, b. 75003, of. 27|  NULL        |
| 516097| Capen-Hall| NY 14260, of. 1611.                     |(716) 645-3321|
+-------+-----------+------------+----------------------------+--------------+

但是我明白了:

 SELECT * FROM CLIENTS_address;
+-------+-----------+-----------------------------------------+--------------+
| zip   | city      | Address                                 | Phone        |
+-------+-----------+-----------------------------------------+--------------+
| 010010| Paris     | Saint-Gilles,  st. Rue, b. 75003, of. 27|  NULL        |
| 516097| Capen-Hall| NY 14260, of. 1611. (716) 645-3321      |(716) 645-3321|
+-------+-----------+------------+----------------------------+--------------+

2 个答案:

答案 0 :(得分:1)

SQLQuery

SELECT CASE
           WHEN CHARINDEX(' ', CLIENTS.adres)>0 THEN SUBSTRING(CLIENTS.adres, 1, CHARINDEX(' ', CLIENTS.adres)-2)
           ELSE adres
       END [ZIP],
       CASE
           WHEN CHARINDEX(',', CLIENTS.adres)>0 THEN SUBSTRING(CLIENTS.adres, 9, CHARINDEX(',', CLIENTS.adres, 9)-9)
           ELSE adres
       END [city],
       CASE
           WHEN CHARINDEX(', ', CLIENTS.adres)>0
                AND CHARINDEX('Phone: ', CLIENTS.adres) > 0 THEN SUBSTRING(CLIENTS.adres, CHARINDEX(',', CLIENTS.adres, 9)+1, CHARINDEX('Phone: ', CLIENTS.adres)-CHARINDEX(',', CLIENTS.adres, 9)-2)
           WHEN CHARINDEX(', ', CLIENTS.adres)>0 THEN SUBSTRING(CLIENTS.adres, CHARINDEX(',', CLIENTS.adres, 9)+1, len(CLIENTS.adres))
           ELSE NULL
       END AS [Address],
       CASE
           WHEN CHARINDEX('Phone', CLIENTS.adres)>0 THEN SUBSTRING(CLIENTS.adres, CHARINDEX('Phone: ', CLIENTS.adres)+7, len(CLIENTS.adres))
           ELSE NULL
       END AS [Phone]
FROM Table1 CLIENTS

结果:

|    ZIP |       city |                                  Address |          Phone |
|--------|------------|------------------------------------------|----------------|
| 010010 |    c.Paris |  Saint-Gilles, st. Rue, b. 75003, of. 27 |         (null) |
| 516097 | Capen-Hall |             Buffalo, NY 14260, of. 1611. | (716) 645-3321 |

答案 1 :(得分:0)

您可以尝试这个。

SELECT 
CASE WHEN CHARINDEX(' ', CLIENTS.adres)>0 THEN 
    SUBSTRING(CLIENTS.adres, 1, CHARINDEX(' ', CLIENTS.adres)-2)
ELSE 
    adres
END [ZIP],
CASE WHEN CHARINDEX(',', CLIENTS.adres)>0 THEN 
    SUBSTRING(CLIENTS.adres, 9, CHARINDEX(',', CLIENTS.adres, 9)-9)
ELSE 
    adres
END [city],
CASE WHEN CHARINDEX(', ', CLIENTS.adres)>0 AND CHARINDEX('Phone: ', CLIENTS.adres) > 0 THEN 
        SUBSTRING(CLIENTS.adres, CHARINDEX(',', CLIENTS.adres, 9)+1, CHARINDEX('Phone: ', CLIENTS.adres)-CHARINDEX(',', CLIENTS.adres, 9)-1)
    WHEN CHARINDEX(', ', CLIENTS.adres)>0 THEN 
         SUBSTRING(CLIENTS.adres, CHARINDEX(',', CLIENTS.adres, 9)+1, len(CLIENTS.adres))
ELSE 
    NULL
END AS [Address],
CASE WHEN CHARINDEX('Phone', CLIENTS.adres)>0 THEN 
    SUBSTRING(CLIENTS.adres, CHARINDEX('Phone: ', CLIENTS.adres)+7, len(CLIENTS.adres))
ELSE
    NULL
END AS [Phone]
FROM Table1 CLIENTS

要进行测试,您可以尝试

 declare @mychar nvarchar(max)
 set @mychar = '516097, Capen-Hall, Buffalo, NY 14260, of. 1611. PHONE: (716) 645-3321'
 select SUBSTRING(@mychar,CHARINDEX(',',@mychar,9)+1,len(@mychar))  ---- your query
 select SUBSTRING(@mychar,CHARINDEX(',',@mychar,9)+1,CHARINDEX('PHONE:', @mychar)-CHARINDEX(',',@mychar,9)-1)   --- porposed solution