Note that there are some explanatory texts on larger screens.

plurals
  1. POXML .value() method issue with CONCAT_NULL_YIELDS_NULL option
    primarykey
    data
    text
    <p>I am trying to convert rows into comma delimited string. Here is my table and data</p> <pre><code>drop table test create table test (a int, b int, c varchar(30), d varchar(30)) insert into test values(1,1,'&lt;1&gt;','&lt;d&gt;') insert into test values(1,1,'&lt;2&gt;','&lt;d&gt;') insert into test values(1,2,'&lt;3&gt;','&lt;d&gt;') insert into test values(1,2,'&lt;4&gt;','&lt;d&gt;') insert into test values(1,3,'&lt;5&gt;','&lt;d&gt;') insert into test values(1,3,'&lt;6&gt;','&lt;d&gt;') </code></pre> <p>I am expecting a result of </p> <pre><code>, &lt;1&gt; - &lt;d&gt;, &lt;2&gt; - &lt;d&gt;, &lt;3&gt; - &lt;d&gt;, &lt;4&gt; - &lt;d&gt;, &lt;5&gt; - &lt;d&gt;, &lt;6&gt; - &lt;d&gt; </code></pre> <p>If I use</p> <pre><code>select (SELECT ',' + ' ' +ltrim(rtrim(tc.c)) +' - '+ ltrim(rtrim(tc.d)) FROM test tc FOR XML PATH(''),type) </code></pre> <p>the query gives the the following result</p> <pre><code>, &amp;lt;1&amp;gt; - &amp;lt;d&amp;gt;, &amp;lt;2&amp;gt; - &amp;lt;d&amp;gt;, &amp;lt;3&amp;gt; - &amp;lt;d&amp;gt;, &amp;lt;4&amp;gt; - &amp;lt;d&amp;gt;, &amp;lt;5&amp;gt; - &amp;lt;d&amp;gt;, &amp;lt;6&amp;gt; - &amp;lt;d&amp;gt; </code></pre> <p>Therefore I added .value method as follows</p> <pre><code>select (SELECT ',' + ' ' +ltrim(rtrim(tc.c)) +' - '+ ltrim(rtrim(tc.d)) FROM test tc FOR XML PATH(''),type).value('(./text())[1]','varchar(max)') </code></pre> <p>This one produces the correct result. Here comes my real issue: It works only when I set CONCAT_NULL_YIELDS_NULL on. If I set CONCAT_NULL_YIELDS_NULL off Sql Server complains with </p> <pre><code>"Msg 1934, Level 16, State 1, Line 1 SELECT failed because the following SET options have incorrect settings: 'CONCAT_NULL_YIELDS_NULL'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations." </code></pre> <p><strong>But I don't want to set CONCAT_NULL_YIELDS_NULL on since it will conflict with my other queries. Can anyone tell me what is the solution to this issue?</strong></p> <p>Thanks in advance.</p> <p>P.S. I saw this in SO</p> <pre><code>SELECT @XYList = @XYList + CONVERT(varchar, X) + ',' + CONVERT(varchar, Y) + ',' FROM POINTS </code></pre> <p>My requirements is to build a view. Therefore I cannot use a solution using variable. </p> <p>My final result looks like this.</p> <pre><code>a b products 1 1 &lt;1&gt; - &lt;d&gt;, &lt;2&gt; - &lt;d&gt; 1 2 &lt;3&gt; - &lt;d&gt;, &lt;4&gt; - &lt;d&gt; 1 3 &lt;5&gt; - &lt;d&gt;, &lt;6&gt; - &lt;d&gt; </code></pre> <p>produced by the query when CONCAT_NULL_YIELDS_NULL is set to on</p> <pre><code>SELECT tp.a, tp.b , STUFF((SELECT ',' + ' ' +ltrim(rtrim(tc.c)) +' - '+ ltrim(rtrim(tc.d)) FROM test tc WHERE tp.a = tc.a AND tp.b = tc.b FOR XML PATH(''), type).value('(./text())[1]','varchar(max)'), 1, 1, '' ) products FROM test tp GROUP BY tp.a, tp.b </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload