Note that there are some explanatory texts on larger screens.

plurals
  1. POISNULL vs CASE Return Type
    primarykey
    data
    text
    <p>I recently ran into an interesting issue with changing <code>CASE</code> statements to <code>ISNULL</code> functions in TSQL. The query I was working with is used to get some user attributes and permissions for a website I work on. Previously the query had a number of <code>CASE</code> statements similar to the following:</p> <p>NOTE: <code>a.column1</code> in the example is of type <code>bit</code> in the table. Also note that the <code>[CanDoSomething]</code> result column is sometimes used as a string in the website.</p> <pre><code>SELECT ... CASE WHEN a.column1 IS NULL THEN 0 ELSE a.column1 END [CanDoSomething] ... FROM a </code></pre> <p>The DBA replaced these <code>CASE</code> statements with the <code>ISNULL</code> function:</p> <pre><code>SELECT ... ISNULL(a.column1, 0) [CanDoSomething] ... FROM a </code></pre> <p>This seems like a fine change, but it caused something unexpected when retrieving the data in C#. With the previous query, the value of the <code>[CanDoSomething]</code> column when accessed from a <code>DataTable</code> in C# was <code>1</code> or <code>0</code>. When we changed to using <code>ISNULL</code>, the value in C# was then changed to <code>true</code> or <code>false</code> which, when treated as a string, are obviously not the same as <code>1</code> or <code>0</code>.</p> <p>The errors this caused have already been addressed. I'm just curious why <code>ISNULL</code> returns a different value than an equivalent <code>CASE</code> statement and I can't seem to find any answers on Google.</p>
    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.
 

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