How to set true or false by checking null in TSQL?

What is the correct syntax for returning TRUE if the field is not NULL and returns FALSE if it is NULL in TSQL?

SELECT -- here return TRUE if table.Code IS NOT NULL. And FALSE otherwise FROM table 
+4
source share
4 answers
  select case when code IS NULL then 'false' else 'true' end as result from the_table 
+5
source

There is no true or false in mssql. You can use the data type 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 
+7
source

For pleasure:

 SELECT ISNULL(NULLIF(ISNULL(code,0),code),1) FROM table 
+3
source

ints are passed to true, therefore:

CAST (ISNULL (int, 0) AS bit)

You can use Length (x) if its string type

+1
source

Source: https://habr.com/ru/post/1381896/


All Articles