MsSql behaves differently when filtering for an empty string by positive and negative query [duplicate] - sql

This question already has answers here:
Issues with SQL comparison and null values
(8 answers)
Closed 2 years ago.
I just realized very strange behavior of my MsSql database when filtering empty string != '' or <> '' regarding NULL values.
I have following data in the table
ID
Value
(table X)
1
(empty string)
2
NULL
(no value)
3
text
(some real text)
This query select * from X where Value = '' results in:
ID: [1]
Both queries select * from X where Value != '' and select * from X where Value <> '' result in:
ID: [3]
What I do not understand is, why 2nd query is not returning ID=2?
I know the syntax for checking explicitly on null values where Value IS NULL so I would expect, that 2nd query would behave differently. When checking for non-empty values, I used to write where Value <> '' AND Value IS NOT NULL. From now on I am totally confused...

NULL does not have a string value - or any value at all, whereas ' ' is an empty string. You're looking for not empty strings in your 2nd query. NULL is not an empty string or a string at all. NULL is a "value" of no other type than NULL itself
It is not possible to test for NULL values with comparison operators,
such as =, <, or <>.
https://www.w3schools.com/sql/sql_null_values.asp

Related

SQL Case statement - Null + Not Null - then return a column [duplicate]

This question already has answers here:
SQL is null and = null [duplicate]
(4 answers)
Closed 11 months ago.
I use SQL inside a 3rd party system (So don't know the type it is)
I am trying to make a CASE work on a column using data from 2 more.
I want to display a column call Channel that is calculated using the following logic:
If column O.Test is blank and column o.subsource is not blank, display 'RESEND', otherwise display the value of column o.Source.
This is part of the SQL showing the CASE I wrote to do this:
select
-- other columns
(CASE
WHEN o.Test = NULL AND o.Subsource IS NOT NULL THEN 'RESEND'
ElSE o.Source
END) o.Source AS 'Channel',
-- other columns
The SQL runs with no errors but the output always shows what is in o.Source.
X = NULL is not equal IS NULL X
Check this question,
SQL is null and = null
change this statement "o.Test = NULL" instead of "o.Test IS NULL"

How to query empty string in postgresql?

I have table, in which a column has empty fields.when i try to run simple query like below it is not
returning the data.
select * from table1 where "colunm1" = ''
But below two queries returns data
1,
select * from table1
where coalesce("colunm1", '') = ''
2,
select * from table1
where "colunm1" is null
Can someone tell me the reason?
TIA
You have describe the behavior of a column that is NULL. NULL is not the same as an empty string.
It fails any equality comparison. However, you can use is null or (less preferentially) coalesce().
The only database that treats an empty string like a NULL value, is Oracle.
Logically, for a DBMS, that would be wrong.
Relational DBMSs are all about :
set theory
Boolean algebra
A NULL value is a value that we don't know. A string literal of '' is a string whose value we know. It is a string with no characters, with a length of 0. We don't know of a NULL as a string how long it is, how many and, if any, which, characters it contains.
So it is only logical that:
the comparison '' = '' will evaluate to TRUE
the comparison NULL = NULL will evaluate to FALSE , as any comparison with a NULL value will evaluate to FALSE.
And functions like COALESCE() or NVL(), IFNULL(), ISNULL() will return the first parameter if it does not contain a NULL value. That is consistent.
Except in Oracle

Why concat a null with text is null? [duplicate]

This question already has answers here:
String concatenation with a null seems to nullify the entire string - is that desired behavior in Postgres?
(4 answers)
Closed 1 year ago.
Why is the result of concatenating a null with a string null in sql? How does it work internally?
If you concatenate a column which an empty value with some text, something like next:
select '' || 'a'
The output would be:
Output = 'a'
But, in the case you concatenates a null value with a text, the result is null
select null || 'a'
The output would be:
Output = NULL
So,
Why am I not getting the same result or getting an error?
Definitively, how does it work internally?
null is not an empty string - it's not even a value. It's a lack thereof. You could think of null as an "unknown", missing value. So the result of concatinating an unknown value with 'a' would be unknown - i.e., null.
If you need to concatenate null values in Postgres, you should use concat function.
According to Postgres documentation:
concat(str "any" [, str "any" [, ...] ])
Concatenate all arguments. NULL arguments are ignored.
select concat('Lady', 2, NULL, 'More');
Returns:
Lady2More
NULL means missing value, it has difference meaning of '', which is blank, blank actually has value but is is blank value, you cannot say NULL has value, it does not exist. Based on this, any operation with NULL will return NULL
NULL is a non-value, a nonexistent value. It is not zero. It is not an empty string. A value cannot equal NULL. No two NULL values are equal.
It's a missing or absent "data" if you have to give it a meaning.
A NULL value is often defined as one that is unknown or not applicable, but even these definitions can be open to debate. For instance, a record may not include a customer’s birthdate because the salesperson didn’t ask or because the customer would not provide it, but the customer still knows that date, so it is hardly unknown, nor is it any less applicable.

Empty string vs NULL

I've a table where some rows have some blank cells. I tried to select such rows using IS NULL function. But the query select 0 rows.
select * from zzz_fkp_registration_female where fname is null;
(0 row(s) affected)
Now I changed my query to:
select * from zzz_fkp_registration_female where fname is null or fname ='';
I got the desired result.
Why is IS NULL not giving the result? What is the difference between IS NULL and '' since the cells are empty. Please explain.
Some differences between them:
NULL can be assigned to any type, as opposed to empty string which won't be compatible with date/numerical fields.
NULL is an UNKNOWN value, it doesn't have a value as opposed to an empty string, which is a value, but empty one.
As far as I'm aware of, NULL shouldn't capture memory as opposed to an empty string which does.
null = null will result in null as opposed to ''='' which will result in TRUE.
You can have your query modified as below:
select * from zzz_fkp_registration_female where isnull(fname,'') = '';
This query will result all the blanks cells as well as cell with null values.
Note:
NULL values represent missing unknown data.
Blank data is actual data entered as blank during input.
Null is an absence of a value. An empty string is a value, but is just empty.
Null is special to a database.
Null has no bounds, it can be used for string, integer, date, etc. fields in a database.
NULL isn't allocated any memory, the string with NUll value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is "".
By using NULL you can distinguish between "put no data" and "put empty data".
Some more differences:
A LENGTH of NULL is NULL, a LENGTH of an empty string is 0.
NULLs are sorted before the empty strings.
COUNT(message) will count empty strings but not NULLs
You can search for an empty string using a bound variable but not for a NULL. This query:
SELECT *
FROM mytable
WHERE mytext = ?
will never match a NULL in mytext, whatever value you pass from the client. To match NULLs, you'll have to use other query
SELECT *
FROM mytable
WHERE mytext IS NULL
He can't find NULL because it doesn't have a value
An empty string is a value, but its empty.
Only if its a value it can compare with another value
It is because a cell containing NULL and an empty cell are 2 different things.
i.e. -> NULL is not as same as '' or "".

Output of "select 1 where NULL <> -1;" [duplicate]

This question already has answers here:
why is null not equal to null false
(7 answers)
Closed 6 years ago.
I guess I don't fully understand the meaning of NULL in SQL. I ran this SQL and I expected to see 1 as the output but I didn't see that:
select 1 where NULL <> -1;
Isn't NULL and -1 different? Can anyone explain why this clause of "NULL <> -1" is FALSE?
NULL Is not a value and therefore cannot be compared with any other value and get any other result than null. Docs. And how to compare nulls.
NULL means unknown. So with where NULL <> -1 you want to know whether the unknown value equals -1. The DBMS does not know (of course), so the result of the expression is neither TRUE nor FALSE; it is NULL.
Only rows for which the WHERE clause results in TRUE are selected. As your expression doesn't result in TRUE but in NULL, there is no row selected.