Querying a null boolean field - sql

When querying a boolean field that is null why does
Select * From MyTableName where [boolfieldX] <> 1
not return any rows with null in [boolfieldX]? 1 <> null I would have expected rows with null to be returned.

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
You have to use the IS NULL and IS NOT NULL operators instead, or you have to use functions like ISNULL() and COALESCE()
Select * From MyTableName where [boolfieldX] <> 1 OR [boolfieldX] IS NULL
OR
Select * From MyTableName where ISNULL([boolfieldX],0) <> 1
Read more about null comparison in Stackoverflow Documentation
Read more about ISNULL() and COALESCE() Functions in Stackoverflow Documentation

I believe that it's because null is an unknown value. You can't query against an unknown 'value'. In my opinion, referring to null as a 'value' is an oxymoron because it represents an unknown. Using the operators "Is Null" and "Not Is Null" in conjunction with whatever selection criteria will return the desired results, or translating it by converting a null an alternate value will work like this: IsNull([boolfield], 'some compatible value')

Hi try to use this query:
select * from mytablename where [boolFieldX] is null And [boolFieldX] <> 1

Related

SQL , SQL QUERIES

I have the following condition:
if column is NULL, or have value like Unassessed or ABC then the result should be I.
I need help how to put three in one condition like below. Just confused with NULL value. And it is string in column.
JobAssessStage ='Unassessed','','ABC'
JobAssessStage in ('Unassessed','','ABC')
Use below condition with is null
JobAssessStage in ('Unassessed','ABC') or JobAssessStage is null
use case when for multiple condition
select case when JobAssessStage in ('Unassessed','ABC') or
JobAssessStage is null then 'I' else 'You' End
from yourtable
In some cases you can use also use COALESCE function (it returns the first non-null expression in a list) on JobAssessStage field:
COALESCE(JobAssessStage, 'X') in ('Unassessed','ABC','X')
If JobAssessStage is NULL with COALESCE the value you're testing becomes X.
I told you "in some cases" because you have to be sure that the value you have chosen (X) is not a value that your field can take.
You can formulate your like below:
SELECT *
FROM table
WHERE 1 = 1
AND (JobAssessStage IN ('Unassessed', 'ABC')
OR JobAssessStage IS NULL);

SQL Coalesce not returning any rows

I am using Postgres and have the following SQL statement:
SELECT *
FROM "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
Which returns one row:
id vehicle_config_id cosy_url image_type_id
113 3 SomeValue 2
When I run the following:
SELECT * from "osmlocal-dsd-de".t_osm_vehicle_image t
WHERE t.vehicle_config_id = 3
and image_type_id = 2
and coalesce(t.cosy_url, '') = ''
Zero rows are returned.
I think my understanding of coalesce is wrong, because I would have expected one row still to be returned, because the cosy_url is not null.
Any advise on what I am doing wrong would be appreciated.
Your understanding of coalesce is wrong
It returns the first argument that is not null. If all arguments are null, the COALESCE function will return null
In your case t.cosy_url is not null it is equally SomeValue and your condition doesn't work because SomeValue is not equal ''
You seem to be misunderstanding coalesce(). It returns the first value that is not null.
In your case, you have:
coalesce(t.cosy_url, '')
Because t.cosy_url has a value ('SomeValue'), this evaluates to that value. The value is not '' so the expression returns false and the entire where clause returns false.
If you want non-NULL values, then use:
t.cosy_url is not null

"!="/NOT perhaps not working properly in SQLite

I have a table with about a hundred rows. It has a column is_gallery that contains either 1, 0, or NULL. If I do...
SELECT * WHERE is_gallery != 1
or
SELECT * WHERE NOT (is_gallery = 1)
it excludes the rows where is_gallery is null. I can manage to get a proper response if I do
SELECT * WHERE (is_gallery = 0 OR is_gallery is null)
But shouldn't the "!=" or NOT work? Isn't there a way to just return the rows where is_gallery doesn't equal 1 without testing for every other possibility?
You can use the IS and IS NOT operators instead of = and !=. These treat NULL like a normal value.
SELECT * FROM yourTable WHERE is_gallery IS NOT 1
The best thing to use is coalesce as in:
SELECT *
WHERE coalesce(is_gallery,0) != 1;
what coalesce does, is replaces any null value in that column with the second parameter. In the example above, any nulls in the "is_gallery" column will be replaced with 0 before it is compared with 1. So will of course return true.
On NULL realize that a NULL value isn't equal to ANYTHING - not even NULL itself. It cannot be compared - so when "comparing", it always will return FALSE. On NULL, it has a special operator which is "IS NULL" or "IS NOT NULL"

Comparing a value to a NULL in t-SQL

I was curious if it's legal in t-SQL to compare a NULL to a value?
For instance, if I have:
WITH ctx AS(SELECT 123 AS n0, NULL AS n1)
SELECT n0 FROM ctx
WHERE ctx.n1 < 130
the WHERE clause in that case is always evaluated as FALSE. Is it something I can rely on?
You can't compare NULL with any other value, it will result in 'UNKNOWN'.
From msdn source
A value of NULL indicates that the value is unknown. A value of NULL
is different from an empty or zero value. No two null values are
equal. Comparisons between two null values, or between a NULL and any
other value, return unknown because the value of each NULL is unknown.
All boolean operations in T-Sql with null value returns 'UNKNOWN', which is recognized as false in clauses. You can use ISNULL function when you want set some default value.
for example in your case:
WITH ctx AS(SELECT 123 AS n0, NULL AS n1)
SELECT n0 FROM ctx
WHERE isnull(ctx.n1,0) < 130
It depends on the value of ANSI_NULLS.
http://msdn.microsoft.com/en-us/library/ms191270%28v=sql.90%29.aspx
When SET ANSI_NULLS is ON, a comparison in which one or more of the
expressions is NULL does not yield either TRUE or FALSE; it yields
UNKNOWN.
Transact-SQL supports an extension that allows for the comparison
operators to return TRUE or FALSE when comparing against null values.
This option is activated by setting ANSI_NULLS OFF. When ANSI_NULLS is
OFF, comparisons such as ColumnA = NULL return TRUE when ColumnA
contains a null value and FALSE when ColumnA contains some value
besides NULL.
The WHERE clause in the following = is also FALSE. You need to be very careful with NULLs
WITH ctx AS
(
SELECT 123 AS n0, NULL AS n1
)
SELECT *
FROM ctx
WHERE ctx.n1 = NULL
I've always used the EXISTS keyword along with EXCEPT like so
SELECT 1
WHERE EXISTS ((SELECT 1) EXCEPT (SELECT NULL))

sql server sql case

What is =1 doing here ?
SELECT TOP 3
NewsId,
NewsTitle,
NewsContent
FROM disc_News
WHERE CASE
WHEN DatePublish IS NOT NULL and DateExpired IS NOT NULL THEN 1
ELSE 0
END = 1
1 is just making the whole expression evaluate to true or false. Where clauses are supposed to evaluate Boolean expressions, otherwise you would have a syntax error
It is getting you the records where DatePublish AND DateExpired are NOT NULL.
The following WHERE clause should be equivalent
WHERE DatePublish IS NOT NULL
AND DateExpired IS NOT NULL