Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Conditional on Bit

Why does this..

DECLARE @SkyBlue Bit
SET @SkyBlue = 1
IF @SkyBlue
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'

Produce this

"An expression of non-boolean type specified in a context where a condition is expected, near 'Select'."

And is there a Boolean type in SQL2008?

like image 230
madcolor Avatar asked Dec 14 '22 02:12

madcolor


1 Answers

@SkyBlue is a bit, not a boolean. Try:

DECLARE @SkyBlue Bit
SET @SkyBlue = 1
IF @SkyBlue = 1
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'

Note that this also fails

if 1
    Select 'the sky is blue!'
ELSE
    Select 'the sky is not blue!'
like image 133
Matthew Vines Avatar answered Dec 25 '22 01:12

Matthew Vines