SQL Server select NULL and false but not true in sql

blmhpbnm  于 2023-08-02  发布在  其他
关注(0)|答案(8)|浏览(105)

i an new in vb.net sql and what i am asking may be silly question. I have a table in sql server 2005 and a column name activated. this column contain NULL, true or false.

I want to select only NULL or false values. How can i do this?

7uzetpgm

7uzetpgm1#

The selection should be done on the SQL Server's side. Your query should look like this:

SELECT *
FROM MyTable
WHERE activated = 0 OR activated is NULL

The above assumes that the column activated is of an integral or a BIT data type.

Note: it may be tempting to use a seemingly equivalent WHERE activated <> 1 condition. This would be incorrect, though, because comparisons of NULL values to anything result in a NULL , so the rows with activated = NULL would be excluded.

m1m5dgzv

m1m5dgzv2#

You should be able to do a coalesce on the field to get it to false if it is null. A coalesce statement checks to see if the first parameter is null, if it is it returns the value in the second parameter. There would be two solutions:

SELECT *
FROM MyTable
WHERE COALESCE(activated,'false') <> 'true'`

--OR--

SELECT *
FROM MyTable
WHERE activated = 'false' or activated is null
cgh8pdjw

cgh8pdjw3#

With SQL's 3-valued logic, comparisons for NULL need to be done using the IS NULL operator... the check for false can be done with = :

SELECT YourColumn
FROM YourTable
WHERE 
  YourColumn IS NULL 
  OR YourColumn = 0
1dkrff03

1dkrff034#

Sometimes you have to see it to believe...

PRINT '0 = 0'       ; IF 0 = 0        PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '0 = 1'       ; IF 0 = 1        PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '0 = NULL'    ; IF 0 = NULL     PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT '1 = NULL'    ; IF 1 = NULL     PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT 'NULL = NULL' ; IF NULL = NULL  PRINT 'true' ELSE PRINT 'false';PRINT '';
PRINT 'NULL IS NULL'; IF NULL IS NULL PRINT 'true' ELSE PRINT 'false';PRINT '';
zed5wv10

zed5wv105#

SELECT * FROM MyTable WHERE isnull(activated, 0) = 0
de90aj5v

de90aj5v6#

For checking NULL you have to use the keyword "IS NULL" and for false =0 like below

SELECT * FROM MyTable WHERE activated = 0 OR activated is NULL
vwoqyblh

vwoqyblh7#

The SQL way is to use IS TRUE or similar:

SELECT * FROM MyTable WHERE activated = 0 IS NOT FALSE
798qvoo8

798qvoo88#

@girgen

Hi, in Sql server this query is not true.

SELECT * FROM MyTable WHERE activated = 0 IS NOT FALSE

This query is for MySql Query not in Sql server. in sql server you can use IS NOT NULL.

相关问题