我有一个下表:
| 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。