Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To expand on @dasblinkenlight's explanation:</p> <p>The real issue is that, per <a href="http://technet.microsoft.com/en-us/library/ms181765.aspx">the documentation</a>, <code>case</code> expressions</p> <blockquote> <p>return the highest precedence type from the set of types in *result_expressions* and the optional *else_result_expression*. For more information, see <a href="http://technet.microsoft.com/en-us/library/ms190309.aspx">Data Type Precedence (Transact-SQL)</a>.</p> </blockquote> <p>While <code>isnull()</code></p> <blockquote> <p>returns the same type as *check_expression*</p> </blockquote> <p>In your case, that is a <code>bit</code>. Your <code>case</code> expression:</p> <pre><code>CASE WHEN a.column1 IS NULL THEN 0 ELSE a.column1 END </code></pre> <p>has execution paths that returns two different data types, an <code>int</code> (the literal value <code>0</code>) and a <code>bit</code> (the value of the column). If you look at the <a href="http://technet.microsoft.com/en-us/library/ms190309.aspx">data type precedence chart</a> linked above, <code>int</code> is of higher precedence than <code>bit</code> and thus the <code>bit</code> value is cast to an <code>int</code>.</p> <p>That is because the conversion from <code>bit</code> to <code>int</code> is a <em>widening conversion</em>. Sql Server's <code>bit</code> type essentially, a 1-bit integer value and so no data is lost. The conversion from <code>int</code> to <code>bit</code> is a <em>narrowing conversion</em> and the possibility of data loss exists (at least in concept).</p> <p>You wouldn't have encountered this problem, if the DBA had used <a href="http://msdn.microsoft.com/en-us/library/ms190349.aspx"><code>coalesce()</code></a> instead of <code>isnull()</code>, which would have been my personal choice, as <code>coalesce()</code> is defined to return a type similar to that of <code>case</code>. <code>coalesce()</code></p> <blockquote> <p>Returns the data type of <em>expression</em> with the highest data type precedence. If all expressions are nonnullable, the result is typed as nonnullable.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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