Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a nullable bit with a default value

I need to select a nullable bit column in a view, but use a default value of FALSE whenever the value is NULL. (For other reasons, I can't add the default value on the source table itself.) Here is what I am doing.

CAST 
(
    CASE 
    WHEN bit_column IS NULL THEN 0 
    ELSE bit_column  
END 
    AS BIT
) AS bit_column,
...

I have to do this on four columns, so I'm wondering if there is a better/more efficient way to do this.

like image 610
xcer Avatar asked Jul 27 '10 18:07

xcer


Video Answer


2 Answers

use the isnull function.

isnull(bit_column, 0)
like image 193
spinon Avatar answered Oct 23 '22 14:10

spinon


SELECT coalesce(bit_column,0) bit_column
like image 34
dcp Avatar answered Oct 23 '22 15:10

dcp