如何通过null检查TSQL将值设置为true或false?

pen*_*ake 4 t-sql null expression

如果字段不为NULL,则返回TRUE的正确语法是什么?如果TSQL中为NULL,则返回FALSE?

SELECT -- here return TRUE if table.Code IS NOT NULL. And FALSE otherwise
FROM table
Run Code Online (Sandbox Code Playgroud)

t-c*_*.dk 9

在mssql中没有真或假.您可以使用数据类型位,并将1视为true,将0视为false:

SELECT CASE WHEN Code IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END as Result
FROM table 
Run Code Online (Sandbox Code Playgroud)


a_h*_*ame 5

 select case 
           when code IS NULL then 'false'
           else 'true'
        end as result
 from the_table
Run Code Online (Sandbox Code Playgroud)