跨多个列拆分文本 - SQL Server

时间:2016-04-27 15:03:35

标签: sql-server split substring charindex

示例:

|                  Detail                                                                                |
|--------------------------------------------------------------------------------------------------------|
|   G31479 /1815 /1844  MOC MCZ 11/4/2015 0:0:0 G31479 /1092 /1648  MOC MCZ 11/4/2015 0:0:0 IROP         |
|   G31054  CGH SDU 11/4/2015 0:0:0 G31058  CGH SDU 11/4/2015 0:0:0 IROP                                 |
|   G37663 /1374    SCL CWB 11/10/2015 0:0:0    G37663 /1920    SCL CWB 2/2/2016 0:0:0  MOVE             |
|   G31490 /1081    SDU RBR 12/10/2015 0:0:0    G31490 /1564    SDU RBR 12/17/2015 0:0:0    MOVE         |
|   G31923 /7660    CWB SCL 11/5/2015 0:0:0 G31923 /7660    CWB SCL 1/29/2016 0:0:0 MOVE                 |

可以创建正则表达式或将文本分成列的东西吗?

等于此表:

| OldNumbers         | OldDeparture | OldArrival | OldDeparuteDate | NewNumbers         | NewDeparture | NewArrival | NewDeparuteDate | Type |                                                                    |
|--------------------|--------------|------------|-----------------|--------------------|--------------|------------|-----------------|------|
| G31479 /1815 /1844 | MOC          | MCZ        | 11/4/2015       | G31479 /1092 /1648 | MOC          | MCZ        | 11/4/2015       | IROP |
| G31054             | CGH          | SDU        | 11/4/2015       | G31058             | CGH          | SDU        | 11/4/2015       | IROP |
| G37663 /1374       | SCL          | CWB        | 11/10/2015      | G37663 /1920       | SCL          | CWB        | 2/2/2016        | MOVE |
| G31490 /1081       | SDU          | RBR        | 12/10/2015      | G31490 /1564       | SDU          | RBR        | 12/17/2015      | MOVE |
| G31923 /7660       | CWB          | SCL        | 11/5/2015       | G31923 /7660       | CWB          | SCL        | 1/29/2016       | MOVE |

我尝试了一些问题,但总是在某些时候中断:

         SELECT TOP 100
          CHARINDEX(BD.Detail, ':')
        , SUBSTRING(BD.Detail,(CHARINDEX('0:0:0',BD.Detail) - 11),11) AS DepartureDateOLD 
        , SUBSTRING(BD.Detail,(CHARINDEX('0:0:0',BD.Detail) - 15),4) AS DepartureOLD 
        , SUBSTRING(BD.Detail,(CHARINDEX('0:0:0',BD.Detail) - 19),4) AS ArrivalOLD
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,11) AS DepartureDateNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 28,4) AS DepartureNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 24,4) AS ArrivalNEW
        , BD.CreatedDate
        , BD.Detail         
     FROM #FINAL_OLD BD

我不知道为什么charindex不适用于0:0:0

修改

此查询的工作取决于日期值:

     SELECT TOP 20
          PATINDEX('%0:0:0%', BD.Detail)
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 11),11) AS DepartureDateOLD 
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 15),4) AS DepartureOLD 
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 19),4) AS ArrivalOLD
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,11) AS DepartureDateNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 28,4) AS DepartureNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 24,4) AS ArrivalNEW
        , BD.CreatedDate
        , BD.Detail         
     FROM #FINAL_OLD BD

对于这一行G31450 /1129 GIG POA 11/4/2015 0:0:0 G31278 GIG POA 11/4/2015 0:0:0 MOVE效果很好。 departurearrival总是有3个字符,但日期有时会有10,9或8个字符,例如1/1/2016 = 8和10/12/2016 = 10个字符,I相信我必须考虑这些空间,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

尝试将您的charindex更改为patindex并查看是否可以帮助您。

SELECT TOP 100
          CHARINDEX(BD.Detail, ':')
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 11),11) AS DepartureDateOLD 
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 15),4) AS DepartureOLD 
        , SUBSTRING(BD.Detail,(PATINDEX('%0:0:0%',BD.Detail) - 19),4) AS ArrivalOLD
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,11) AS DepartureDateNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 28,4) AS DepartureNEW 
        , SUBSTRING(BD.Detail,LEN(BD.Detail) - 24,4) AS ArrivalNEW
        , BD.CreatedDate
        , BD.Detail         
     FROM #FINAL_OLD BD

答案 1 :(得分:0)

我不知道是否有更好的方法,但它有效。

     SELECT
         ( CASE 
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 10,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 9,8)
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 11,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 10,9)
             ELSE SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 11,10)
          END) AS [DepartureDateOLD]    
       , ( CASE 
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 10,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 17,3)
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 11,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 18,3)
             ELSE SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 19,3)
          END) AS [DepartureOLD]
        , ( CASE 
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 10,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 13,3)
             WHEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 11,1) = ' ' THEN SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 14,3)
             ELSE SUBSTRING(BD.Detail,PATINDEX('%0:0:0%',BD.Detail) - 15,3)
          END) AS [ArrivalOLD]
        , ( CASE 
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 19,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 18,8)
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 19,9)
             ELSE SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,10)
          END) AS [DepartureDateNEW]            
        , ( CASE 
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 19,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 26,3)
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 27,3)
             ELSE SUBSTRING(BD.Detail,LEN(BD.Detail) - 28,3)
          END) AS [DepartureNEW]
        , ( CASE 
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 19,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 22,3)
             WHEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 20,1) = '    ' THEN SUBSTRING(BD.Detail,LEN(BD.Detail) - 23,3)
             ELSE SUBSTRING(BD.Detail,LEN(BD.Detail) - 24,3)
          END) AS [ArrivalNEW]          
        , BD.CreatedDate
        , BD.Detail         
     FROM #FINAL_OLD BD