Postgresql NOT NULL AND <> '' vs NOT NULL OR <> '' -


i have read lots difference between not null vs <>''

the best explanation found in:

https://www.postgresql.org/message-id/aanlktilesutieukzcx9vc14kcifiuvfbrre-yen4k_zi@mail.gmail.com states:

null means question hasn't been answered yet, there no answer, there may answer once provided can't answer empty. empty mean answer has been given , answer empty.

for table working on, trying filter results valid postcodes only (postcode varchar in specific table), tried following:

select postcode customer_table_1  postcode not null or postcode <> ''; 

however gives blank postcodes in results. breaking down ...

select postcode customer_table_1  postcode not null; 

gives blank postcodes whereas

select postcode customer_table_1  postcode <>''; 

only gives valid postcodes in result. therefore not null part of query isn't doing thought was.

as part of more complicated query, have used:

select postcode customer_table_1  postcode not null , postcode <> ''; 

and have achieved desired result. have thought should

select postcode customer_table_1  postcode not null or postcode <> ''; 

because looking records have valid postcode (i.e. not null or not empty strings) should these not connected or statement rather and? i'm not looking postcodes both null , empty strings, 1 or other.

apologies if stupid question, using , doesn't seem logical me , don't want blindly without understanding process behind result. thought understood difference between not null , <>'' things postgres related, more delve more realize don't know!

you benefit knowing de morgan's law fundamental boolean logic.

in case, condition not expressed as:

(not null) or (not empty) 

but actually:

not (null or empty) 

which expanded out according de morgan becomes:

(not null) , (not empty) 

hence why and correct (and indeed "logical") operator.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -