Sql server 按行号筛选SQL Server中的特定单词
我的表格如下:Sql server 按行号筛选SQL Server中的特定单词,sql-server,substring,charindex,Sql Server,Substring,Charindex,我的表格如下: | Row | RecordLocator | Comment |-----|---------------|------------------------------------------------------------------ | 1 | AAA111 | SearchCrt:ONEWAY - EMAIL:0:xffz@hotmail.com - EMAIL:1:bzari@hotmail.com - Pa
| 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
添加第1行所需的电子邮件:0:
,添加第2行所需的电子邮件:1:
。。。。但只有电子邮件。
如果未找到电子邮件,则显示一条消息未找到
我尝试了以下查询:
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。这里有另一种方法,可以与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
无论如何,如果OP能够规范化他们的数据,那就更好了。