• Jeff Moden (6/25/2012)


    ChrisM@Work (6/25/2012)


    This works quite nicely too:

    SELECT

    EmployeeID,

    Email,

    DomainLHS = SUBSTRING(Email, x.PosStart,x.PosEnd-x.PosStart),

    DomainRHS = SUBSTRING(Email, y.PosStart,y.PosEnd-y.PosStart)

    FROM @t

    CROSS APPLY (

    SELECT PosStart = 1+CHARINDEX('@',EMAIL,1),

    PosEnd = NULLIF(CHARINDEX('.',EMAIL,CHARINDEX('@',EMAIL,1)),0)

    ) x

    CROSS APPLY (

    SELECT PosStart = NULLIF(1+CHARINDEX('@',EMAIL,x.PosEnd),1),

    PosEnd = NULLIF(CHARINDEX('.',EMAIL,CHARINDEX('@',EMAIL,x.PosEnd)),0)

    ) y

    Absolutely awesome, Chris. "Cascading Cross Applys" (cCA for short! :-D). Looks like it might beat the tar out of cascading CTE's of a similar nature. To be honest, I didn't know such a thing was possible because I didn't even consider that one CA might be able to use the output of another. This crazy Monday has turned into something good after all!

    You should write a "Spackle" article on it, Chris.

    Gosh :blush: thanks Jeff!

    I might have to do just that ๐Ÿ˜‰

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden