I recently ran into an interesting issue with changing CASE statements to ISNULL 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 CASE statements similar to the following:
NOTE: a.column1 in the example is of type bit in the table. Also note that the [CanDoSomething] result column is sometimes used as a string in the website.
SELECT
...
CASE
WHEN a.column1 IS NULL THEN 0
ELSE a.column1
END [CanDoSomething]
...
FROM
a
The DBA replaced these CASE statements with the ISNULL function:
SELECT
...
ISNULL(a.column1, 0) [CanDoSomething]
...
FROM
a
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 [CanDoSomething] column when accessed from a DataTable in C# was 1 or 0. When we changed to using ISNULL, the value in C# was then changed to true or false which, when treated as a string, are obviously not the same as 1 or 0.
The errors this caused have already been addressed. I'm just curious why ISNULL returns a different value than an equivalent CASE statement and I can't seem to find any answers on Google.