I have 2 column ID and description.I want to get the only mailid for each ID column.

我有两个列ID和描述。我想为每个ID列找到唯一的mailid。

ID  Description
1   I have 2 mailID please note this mai1: anto1@gmail.com and mai1: anto2@gmail.com and mai1: anto3@gmail.com abbaaabbbbbbb.
2   I have 2 mailID please note this mai1: sample1@gmail.com and mai1: sample2@gmail.com and mai1: sample3@gmail.com abbaaabbbbbbb.

Expected output

预期的输出

ID  Description
1   anto1@gmail.com
1   anto2@gmail.com
1   anto3@gmail.com
2   sample1@gmail.com
2   sample2@gmail.com
2   sample3@gmail.com

I have tried this below query.

我尝试过以下查询。

SELECT id,Description
FROM sample_for
WHERE CHARINDEX('@', Description) > 0

But please provide the alternate valid query.

但请提供另一个有效查询。

4 个解决方案

#1


2

Maybe something like this...

也许是这样的…

Test Data

Declare @table TABLE(ID int , Description Varchar(8000))
INsert into @table values
(1 ,  'I have 2 mailID please note this mai1: anto1@gmail.com and mai1: anto2@gmail.com and mai1: anto3@gmail.com abbaaabbbbbbb'),
(2 ,  'I have 2 mailID please note this mai1: sample1@gmail.com and mai1: sample2@gmail.com and mai1: sample3@gmail.com abbaaabbbbbbb')

Query

Select ID
     ,LEFT(RTRIM(LTRIM(Emails)) , CHARINDEX(' ' , RTRIM(LTRIM(Emails)))) Emails
from 
(
SELECT t.ID 
          ,Split.a.value('.', 'VARCHAR(100)') Emails
FROM   
    (SELECT Cast ('<X>' + Replace(Description, ':', '</X><X>') + '</X>' AS XML) AS Data
            ,ID
    FROM    @table
    ) AS t CROSS APPLY Data.nodes ('/X') AS Split(a)
 )a 
Where a.emails LIKE '%@%'

Result Set

╔════╦════════════════════╗
║ ID ║       Emails       ║
╠════╬════════════════════╣
║  1 ║ anto1@gmail.com    ║
║  1 ║ anto2@gmail.com    ║
║  1 ║ anto3@gmail.com    ║
║  2 ║ sample1@gmail.com  ║
║  2 ║ sample2@gmail.com  ║
║  2 ║ sample3@gmail.com  ║
╚════╩════════════════════╝

更多相关文章

  1. PLSQL乱码解决方案
  2. 一种在t-SQL中选择两个日期之间的日期的方法
  3. 数据库不支持中文解决方案(mysql)
  4. 找到另一个列至少有两个不同值的id
  5. Mysql替代解决方案Cassandra
  6. [置顶] Android屏幕适配解决方案
  7. 物流货运移动APP解决方案
  8. 如何在函数中将两个参数从1个类传递给另一个?
  9. 转:Android Studio Error:Connection timed out: connect.解决方

随机推荐

  1. 【图解数据结构】 一组动画彻底理解二叉
  2. 每天一算:Delete Node in a Linked List
  3. 【图解数据结构】 一组动画彻底理解基数
  4. 【图解数据结构】 一组动画演示冒泡排序
  5. 每天一算:Reverse Linked List
  6. 每天一算:Reverse Linked List II
  7. 国内最具影响力科技创投媒体36Kr的容器化
  8. 十大经典排序算法动画,看我就够了!
  9. Mybatis002-CRUD
  10. 这些奇葩的排序算法,你没见过动画吧?