|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: 2 days ago @ 7:52 AM
Points: 1,710,
Visits: 61
|
|
| That is so cool. I love it.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 1:49 AM
Points: 3,123,
Visits: 4,310
|
|
Juan de Dios (2/16/2010) This script is very useful when all the rows are not NULL, but if you modify the insert of Mark with NULL, the script only returns Bill.
insert into @table values (4, NULL,'M')
Result: Bill
Where John goes???
Remember, in SQL, anything concat NULL = NULL, therefore, when the NULL is encountered, the value is made NULL, then the next string is added. Since the definition is ISNULL(@var,''), the next value is added to the now empty string, delivering the last value
To cate for this, an aditional clause should be added to the predicate, viz:
AND Name IS NOT NULL
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:16 AM
Points: 480,
Visits: 221
|
|
Nice question. I guessed at the answer but lucked out.
I already have a use for this in a couple of procedures today. PERFECT TIMING!!
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 12:56 PM
Points: 987,
Visits: 124
|
|
| As a couple of comments stated, the names may not come back in the order you want. On the same server, I ran this query at different times and got "John, Mark, Bill", "John, Bill, Mark" and "Bill, John, Mark" .
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 8:55 AM
Points: 5,099,
Visits: 20,190
|
|
Juan de Dios (2/16/2010) -------------------------------------------------------------------------------- This script is very useful when all the rows are not NULL, but if you modify the insert of Mark with NULL, the script only returns Bill.
insert into @table values (4, NULL,'M')
Result: Bill
Where John goes??? stewartc-708166 To cate for this, an aditional clause should be added to the predicate, viz: AND Name IS NOT NULL
In addition or an alternative to stewartc excellent suggestion, you can in effect negate the presence of the NULL by using an ORDER BY [name] clause
More interesting try the code with a BLANK name, without the ORDER BY clause it appears in the list of names as , , - with the ORDER BY clause it appears as a leading comma before Bill.
sanbornd An EXCELLENT QOD thanks for devising it and submitting, may I encourage you to submit additional QODs
If everything seems to be going well, you have obviously overlooked something.
Ron
Please help us, help you -before posting a question please read Before posting a performance problem please read
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 8:25 AM
Points: 540,
Visits: 245
|
|
Here's another alternative using STUFF and FOR XML PATH that does not require a parameter (I believe this is from an article from SSC. If I could have found the URL I would have included it in this post in order to give the appropriate credit.)
SELECT STUFF((SELECT ', ' + name FROM @table WHERE gender = 'M' FOR XML PATH('')), 1, 2, '')
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977,
Visits: 1,499
|
|
A really good QotD! Thanks.
Tom Garth Vertical Solutions
"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:46 PM
Points: 18,732,
Visits: 12,329
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, November 13, 2012 9:44 AM
Points: 156,
Visits: 89
|
|
| In addition to the comment about the missing order by, the result of this is also dependent on the CONCAT_NULL_YIELDS_NULL option. If this option is OFF, the argument to IsNull() will NOT be null, and therefore the result would be: ", John, Mark, Bill" -- leading comma.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, December 07, 2010 12:55 AM
Points: 771,
Visits: 504
|
|
Excellent question.
And thanks all for the additional information in your posts.
-- Kevin C.
|
|
|
|