Convert IS NULL to BIT

diiN__________ picture diiN__________ · Apr 27, 2016 · Viewed 9.3k times · Source

This might be a very basic question but I just came over it while writing a query.

Why can't SQL Server convert a check for NULL to BIT? I was thinking about something like this:

DECLARE @someVariable INT = NULL;
-- Do something
SELECT CONVERT(BIT, (@someVariable IS NULL))

The expected outcome would then be either 1 or 0.

Answer

Gordon Linoff picture Gordon Linoff · Apr 27, 2016

Use case:

SELECT CONVERT(BIT, (CASE WHEN @someVariable IS NULL THEN 1 ELSE 0 END))