按行号过滤SQL Server中的特定单词

时间:2015-07-16 15:17:04

标签: sql-server substring charindex

我有一张桌子:

| 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

我尝试了以下查询:

-

但没有成功,我需要在CHARINDEX字符中停止,foreach ($materiauxlist as $stock) { foreach ($dimensionslist as $dimension) { $besoin = $em->getRepository('TGComptaBundle:Besoin')->findBesoins($stock, $dimension); $besoins[] = $besoin; } } 也不能正常工作。

我正在使用SQL Server 2008。

1 个答案:

答案 0 :(得分:2)

这是另一种方法,它与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