1

我有一个下表:

| Row | RecordLocator | Comment    
|-----|---------------|------------------------------------------------------------------    
|  1  |     AAA111    | SearchCrt:ONEWAY   -   EMAIL:0:xffz@hotmail.com   -   EMAIL:1:bzari@hotmail.com   -   PassangerContact: xxxyy  - Promo:0257 - ......
|  2  |     AAA111    | SearchCrt:ONEWAY   -   EMAIL:0:xffz@hotmail.com   -   EMAIL:1:bzari@hotmail.com   -   PassangerContact: xxxyy  - Promo:0257 - ......
|  1  |     BBB111    | PassangerContact: jrte   -  PersonID:12  -  EMAIL:0:new2015@hotmail.com   -   BagTag: 12315 - .......
|  1  |     CCC111    | Promo:5474   -  EMAIL:0:hot2015@hotmail.com   -   BagTag: 12315 -  EMAIL:1:old2015@hotmail.com -  EMAIL:2:1232015@hotmail.com - .......
|  2  |     CCC111    | Promo:5474   -  EMAIL:0:hot2015@hotmail.com   -   BagTag: 12315 -  EMAIL:1:old2015@hotmail.com -  EMAIL:2:1232015@hotmail.com - .......
|  3  |     CCC111    | Promo:5474   -  EMAIL:0:hot2015@hotmail.com   -   BagTag: 12315 -  EMAIL:1:old2015@hotmail.com -  EMAIL:2:1232015@hotmail.com - .......
|  1  |     DDD111    | Promo:89474   - BagTag: 147515 - dds2121sdsd1a2 -  221221gdfgf - .......

我需要下面的结果:

| Row | RecordLocator | E-mail    
|-----|---------------|------------------------------------------------------------------    
|  1  |     AAA111    | xffz@hotmail.com
|  2  |     AAA111    | bzari@hotmail.com
|  1  |     BBB111    | new2015@hotmail.com
|  1  |     CCC111    | hot2015@hotmail.com
|  2  |     CCC111    | old2015@hotmail.com
|  3  |     CCC111    | 1232015@hotmail.com
|  1  |     DDD111    | not found

EMAIL:0:必要的添加,line 1必要EMAIL:1:的添加line 2....但只有电子邮件。如果找不到电子邮件,则显示一条消息not found

我尝试了以下查询:

SELECT DISTINCT 
            tmpRow.Row
            , tmpRow.RecordLocator
            , isNull(searchEmail.Email, 'not found')
         FROM #TmpRow tmpRow

         CROSS APPLY
         (

            SELECT SUBSTRING ( tmpRow2.Comment , (CHARINDEX('EMAIL:'+ (tmpRow2.Row - 1) +':', tmpRow2.Comment)) , 20 ) AS Email 
                FROM #TmpRow tmpRow2
            WHERE tmpRow.Row = tmpRow2.Row
              AND tmpRow.RecordLocator = tmpRow2.RecordLocator

         )searchEmail

-但是没有成功,我需要在char 中停下来,并且CHARINDEX不工作。

我正在使用 SQL Server 2008。

4

1 回答 1

1

这是与 cte 中的示例数据一起执行此操作的另一种方法。

with something(RowNum, RecordLocator, Comment) as
(
    select 1, 'AAA111', 'SearchCrt:ONEWAY - EMAIL:0:xffz@hotmail.com - EMAIL:1:bzari@hotmail.com - PassangerContact: xxxyy - Promo:0257 - ......' union all
    select 2, 'AAA111', 'SearchCrt:ONEWAY - EMAIL:0:xffz@hotmail.com - EMAIL:1:bzari@hotmail.com - PassangerContact: xxxyy - Promo:0257 - ......' union all
    select 1, 'BBB111', 'PassangerContact: jrte - PersonID:12 - EMAIL:0:new2015@hotmail.com - BagTag: 12315 - .......' union all
    select 1, 'CCC111', 'Promo:5474 - EMAIL:0:hot2015@hotmail.com - BagTag: 12315 - EMAIL:1:old2015@hotmail.com - EMAIL:2:1232015@hotmail.com - .......' union all
    select 2, 'CCC111', 'Promo:5474 - EMAIL:0:hot2015@hotmail.com - BagTag: 12315 - EMAIL:1:old2015@hotmail.com - EMAIL:2:1232015@hotmail.com - .......' union all
    select 3, 'CCC111', 'Promo:5474 - EMAIL:0:hot2015@hotmail.com - BagTag: 12315 - EMAIL:1:old2015@hotmail.com - EMAIL:2:1232015@hotmail.com - .......' union all
    select 1, 'DDD111', 'Promo:89474 - BagTag: 147515 - dds2121sdsd1a2 - 221221gdfgf - .......'
)

select RowNum
    , RecordLocator
    , case when CHARINDEX('EMAIL:' + cast(RowNum - 1 as char(1)), Comment) > 0 
        then SUBSTRING(Comment, CHARINDEX('EMAIL:' + cast(RowNum - 1 as char(1)), Comment) + 8, CHARINDEX(' ', Comment, CHARINDEX('EMAIL:' + cast(RowNum - 1 as char(1)), Comment)) - CHARINDEX('EMAIL:' + cast(RowNum - 1 as char(1)), Comment) - 8)
        else 'not found' 
    end as Email
from something
于 2015-07-16T15:47:21.890 回答