Extract email address from string using tsql

后端 未结 6 1423
面向向阳花
面向向阳花 2021-01-13 09:01

I\'m trying to extract email addresses from an existing comments field and put it into its own column. The string may be something like this \"this is an example comment wit

6条回答
  •  被撕碎了的回忆
    2021-01-13 09:27

    DECLARE @t TABLE (row_id INT, email VARCHAR(100))
    
    INSERT @t (row_id, email)
    VALUES (1, 'drgkls, info@gvi.com, @ dgh507-16-65@'),
            (2, 'hjshfkjshfj@kjs.kjsehf herwfjewr@kjsd.com adjfhja@.com u3483dhj@hhb@.dfj'),
            (3, 'kjsdghfjs4254.23detygh@jhjdfg.dgb лдоврывплдоо isgfsi@ klsdfksdl@,dd.com')
    
    DECLARE @pat VARCHAR(100) = '%[^a-z0-9@._ ]%';
    
    WITH f AS (
             SELECT    row_id,
                     CAST(' ' + email + ' ' AS VARCHAR(102)) email,
                     SUBSTRING(email, PATINDEX(@pat, email), 1) bad,
                     PATINDEX(@pat, email) pat
             FROM    @t
             UNION ALL
             SELECT    row_id,
                     CAST(REPLACE(email, bad, ' ') AS VARCHAR(102)),
                     SUBSTRING(REPLACE(email, bad, ' '), PATINDEX(@pat, REPLACE(email, bad, ' ')), 1) bad,
                     PATINDEX(@pat, REPLACE(email, bad, ' '))
             FROM    f
             WHERE    PATINDEX(@pat, email) > 0
         ),
         s AS 
         (
             SELECT    row_id,
                     email, PATINDEX('%@%', email) pos 
             FROM    f 
             WHERE    pat = 0
                     AND    PATINDEX('%@%', email) > 0
             UNION ALL
             SELECT    row_id,
                     SUBSTRING(email, pos + 1, 102), 
                     PATINDEX('%@%', SUBSTRING(email, pos + 1, 102))
             FROM    s
             WHERE    PATINDEX('%@%', SUBSTRING(email, pos + 1, 102)) > 0
         )
    
    SELECT  row_id, o1 + pp
    FROM    s   
            CROSS APPLY (SELECT    REVERSE(LEFT(email, pos -1)) s1) x
            CROSS APPLY (SELECT    CHARINDEX(' ', s1) i1) y
            CROSS APPLY (SELECT    REVERSE(LEFT(s1, i1 -1)) o1 WHERE i1 > 0) z
            CROSS APPLY (SELECT    CHARINDEX(' ', email, pos) i2) e
            CROSS APPLY (SELECT    SUBSTRING(email, pos, i2 -pos) pp WHERE    i2 > pos + 1) q
    WHERE    LEN(o1) > 1
            AND CHARINDEX('.', pp) > 0
            AND PATINDEX('%@%@%', pp) = 0
            AND PATINDEX('%@.%', pp) = 0
            AND PATINDEX('%.', pp) = 0
    

提交回复
热议问题