sql - 如何查找大描述值中的字符串

标签 sql sql-server

我有 2 列 ID 和描述。我想获取每个 ID 列的唯一 mailid。

ID  Description
1   I have 2 mailID please note this mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16777862792756717b777f7a3875797b" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3929d879cc1b3949e929a9fdd909c9e" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceafa0baa1fd8ea9a3afa7a2e0ada1a3" rel="noreferrer noopener nofollow">[email protected]</a> abbaaabbbbbbb.
2   I have 2 mailID please note this mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb889a968b979ecabb9c969a9297d5989496" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9c8e829f838addaf88828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d6e7c706d71782e5d7a707c7471337e7270" rel="noreferrer noopener nofollow">[email protected]</a> abbaaabbbbbbb.

预期输出

ID  Description
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4829263c2779082f25292124662b2725" rel="noreferrer noopener nofollow">[email protected]</a>
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3b342e35681a3d373b333674393537" rel="noreferrer noopener nofollow">[email protected]</a>
1   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcec1dbc09cefc8c2cec6c381ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a>
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a697b776a767f2b5a7d777b737634797577" rel="noreferrer noopener nofollow">[email protected]</a>
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b283a362b373e691b3c363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>
2   <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5d4f435e424b1d6e49434f4742004d4143" rel="noreferrer noopener nofollow">[email protected]</a>

我已经尝试过以下查询。

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

但请提供替代的有效查询。

最佳答案

也许是这样的......

测试数据

Declare @table TABLE(ID int , Description Varchar(8000))
INsert into @table values
(1 ,  'I have 2 mailID please note this mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1879766c7729587f75797174367b7775" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0f001a015c2e09030f0702400d0103" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8f809a81ddae89838f8782c08d8183" rel="noreferrer noopener nofollow">[email protected]</a> abbaaabbbbbbb'),
(2 ,  'I have 2 mailID please note this mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b786a667b676e3a4b6c666a626725686466" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7704161a071b124537101a161e1b5914181a" rel="noreferrer noopener nofollow">[email protected]</a> and mai1: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9dac8c4d9c5cc9ae9cec4c8c0c587cac6c4" rel="noreferrer noopener nofollow">[email protected]</a> abbaaabbbbbbb')

查询

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 '%@%'

结果集

╔════╦════════════════════╗
║ ID ║       Emails       ║
╠════╬════════════════════╣
║  1 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d2c2339227c0d2a202c2421632e2220" rel="noreferrer noopener nofollow">[email protected]</a>    ║
║  1 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b6b9a3b8e597b0bab6bebbf9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a>    ║
║  1 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7f706a712d5e79737f7772307d7173" rel="noreferrer noopener nofollow">[email protected]</a>    ║
║  2 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccbfada1bca0a9fd8caba1ada5a0e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>  ║
║  2 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b1a3afb2aea7f082a5afa3abaeeca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>  ║
║  2 ║ <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6112000c110d045221060c00080d4f020e0c" rel="noreferrer noopener nofollow">[email protected]</a>  ║
╚════╩════════════════════╝

关于sql - 如何查找大描述值中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228064/

相关文章:

MySQL 将列更改为唯一

mysql - 如何以编程方式选择要在 WHERE 条件中使用的列?

SQL Server : IF EXISTS ; ELSE

c# - 具有指向单个表的多个一对多关系的 Entity Framework 代码优先配置

c# - 以 XML 形式存储的查询字符串属性

mysql - 如何排除在同一 start_dt 上的数据中创建循环的记录?

mysql - SQL 查询帮助,数据现在显示在 1 列中

SQL Server : the maximum recursion 100 has been exhausted before statement completion

sql-server - 如何在 SQL Server 2005 中从 IndexId 获取索引名称

sql - 将 "bad"数据插入 SQL 数据库?