|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 11:54 AM
Points: 749,
Visits: 3,767
|
|
I must be missing something really simple here, but what happened to the last comma? I would think that the result would have to end in a comma since what is stringed together ends in a comma -> @MaleNames + ', '
Never mind - I see that it actiually adds the [name] after the ISNULL funtion.
______________________________________________________________________ The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:21 AM
Points: 5,244,
Visits: 7,062
|
|
The query uses IsNull(@MaleNames + ', ','') + [name].
When @MaleNames is NULL, @MaleNames + ', ' is NULL as well, so the ISNULL kicks in and replaces the NULL with an empty string, to which [name] is concatenated. This leaves us with a single name and no comma.
When @MaleNames is not NULL (but, for instance, a single name and no comma), @MaleNames + ', ' adds a comma to that name, the ISNULL does nothing, and then [Name] is concatenated - so now we have two names, seperated by a comma.
After that it continues to add ', ' and a name to the string, until processing stops.
DISCLAIMER: This description is based on the assumption that SQL Server will process the rows one by one and not reset the variable in between; this is often observed but undocumented behaviour and should not be relied on. Other behaviour can happen anytime (and has in fact sometimes been observed).
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 11:54 AM
Points: 749,
Visits: 3,767
|
|
Thanks Hugo, I should have mulled over that question just a few more minutes before asking it.
______________________________________________________________________ The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:03 AM
Points: 10,990,
Visits: 10,543
|
|
Hugo Kornelis (2/26/2010) I'll raise the red flag (if I only knew how to get red text here):
THIS METHOD IS NOT DOCUMENTED, NOT SUPPORTED, AND NOT GUARANTEED TO RETURN WHAT YOU EXPECT Like that
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|