Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a value to true or false by null check in TSQL?

What is the right syntax to return TRUE if the field is not NULL and to return FALSE if it is NULL in TSQL?

SELECT -- here return TRUE if table.Code IS NOT NULL. And FALSE otherwise
FROM table
like image 753
pencilCake Avatar asked Nov 18 '11 12:11

pencilCake


1 Answers

There is no true or false in mssql. You can use the datatype bit and consider 1 as true and 0 as false:

SELECT CASE WHEN Code IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END as Result
FROM table 
like image 162
t-clausen.dk Avatar answered Oct 11 '22 14:10

t-clausen.dk