• dwain.c (6/24/2012)


    This is not guaranteed to work in all cases (e.g., where @ may appear outside of an email string) but using Jeff Moden's DelimitedSplit8K string splitter, you can try something like this:

    DECLARE @t TABLE (EmployeeID INT, Email VARCHAR(200))

    INSERT INTO @t

    SELECT 1, '<mmooney@gmail.com>;"mmoney@gmail.com"'

    UNION ALL SELECT 2, '<chrisgardener@yahoo.com>)'']chrisgardener@comcast.net)]'

    UNION ALL SELECT 3, '"natan@hotmail.com'':>"natan@hotmail.com?'''

    UNION ALL SELECT 4, '"jennifer@walmart.com}'';jennifer@fedex.com'

    SELECT EmployeeID, Email

    FROM (

    SELECT EmployeeID, Email, ItemNumber

    ,df=CASE WHEN PATINDEX('%[^0-9A-Za-z@.]%', Item) = 0

    THEN Item

    ELSE SUBSTRING(Item, 1, PATINDEX('%[^0-9A-Za-z@.]%', Item) - 1) END

    FROM @t

    CROSS APPLY dbo.DelimitedSplit8K(Email, '@') ds

    WHERE CHARINDEX('.', Item) <> 0) x

    GROUP BY EmployeeID, Email

    HAVING MAX(df) = MIN(df)

    The referenced string splitter can be found here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

    Note that mine won't work in the case where a period (.) appears in the left part of the email name. If you were to provide additional test data, I think I could work around that. The original test data you gave us was pretty messy and I was afraid the real data would be even worse.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St