按行号过滤 SQL 服务器中的特定单词
Filter a specific word in SQL Server by row number
我下面有一个table:
| 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
也不起作用。
我正在使用 SQL Server 2008。
这是在 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
我下面有一个table:
| 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
也不起作用。
我正在使用 SQL Server 2008。
这是在 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