• drew.allen (7/21/2016)


    Sean Lange (7/21/2016)


    drew.allen (7/21/2016)


    Get rid of the ISNULL altogether. FOR XML automatically discards null expressions unless the XSINIL directive is specified.

    SELECT DISTINCT STUFF(

    (SELECT ', ' + t2.Name

    FROM #Temp t2

    where t1.ID = t2.ID -- t1.DeliverableITEP = t2.DeliverableITEP AND t1.GeoMarket = t2.GeoMarket AND t1.Segment = t2.Segment

    FOR XML PATH (''))

    , 1, 1, '') AS Name

    FROM #Temp t1

    Drew

    PS: The estimated plan shows that this is 0.1% faster than using a WHERE clause to exclude NULL values. Of course, I don't put much stock in the estimated plan on such a small table.

    Drew this has the problem when there is an empty string for name.

    That's what I get for posting when I had trouble sleeping last night. Still, I wonder how it might perform with this change.

    SELECT DISTINCT STUFF(

    (SELECT ', ' + NULLIF(t2.Name, '')

    FROM #Temp t2

    where t1.ID = t2.ID -- t1.DeliverableITEP = t2.DeliverableITEP AND t1.GeoMarket = t2.GeoMarket AND t1.Segment = t2.Segment

    FOR XML PATH (''))

    , 1, 1, '') AS Name

    FROM #Temp t1

    Unfortunately, I don't have time to do a comparison.

    Drew

    LOL. Using the NULLIF or a where predicate to remove NULL seems to be the same thing in the end. Both your query and mine produce identical execution plans. 😀

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/