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

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.

Related

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

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

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

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 "".

Oracle : IN and OR

I've a scenrio which process many data in Oracle database. In some cases, the variable Sec_email will contain many values and in some cases Sec_email will contain null or ' '.
so can please any one tell me how to write a query for this?
I tried with
(C.SECONDARY_EMAIL IN ('?,?') OR '' = '' )
where C is the Client table.
When I use this i get the count as 0.
You can perform a not null check before the IN comparison like
Sec_email is not null and C.SECONDARY_EMAIL IN (...
One obvious problem is that Oracle (by default) treats empty strings as NULL. So: '' = '' is the same as NULL = NULL, which is never true.
Arrgh.
In any case, you are probably constructing the query, so use is null instead:
(C.SECONDARY_EMAIL IN ('?,?') OR '' IS NULL
I think the real problem, though, is the first comparison. The IN list has one element with a constant, not two (but perhaps that is your intention). If you want to put a variable number of values for comparison, one method uses regular expressions. For instance:
C.SECONDARY_EMAIL REGEXP_LIKE '^val1|val2|val3$' or '' IS NULL
If you would like to get a list of values when some of them is null you should use:
("some other conditions" OR C.SECONDARY_EMAIL IS NULL)
The question is if it is not null and not ' ' value what you are expecting, if it should be some king of
pattern you should use regular expression:
regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
Also, if you have a few conditions in where clause use should use brackets to group you conditions null check and another one.
All conditions i this case will be divided by OR.
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$'))
or
(C.SECONDARY_EMAIL IS NULL OR regexp_like(C.SECONDARY_EMAIL, '^(.+?[,]+?)+$')
OR C.SECONDARY_EMAIL = ' ')

Oracle sql null value is not selected

In NAME table FIRST column is having null but no rows are selected. Please help me to understand.
SELECT * FROM NAME WHERE FIRST != '1'
Any comparison with null is false - = and <>, >, <, and so on. You cannot use null in an IN list as well - it would be ignored. Moreover, two nulls are not even equal to each other.
To get the nulls, you need to ask for them explicitly, like this:
SELECT * FROM NAME WHERE FIRST IS NULL OR FIRST != '1'
Any comparison to NULL returns NULL, which is equivalent to FALSE. This is true eve of not-equals.
If you want to include NULL values, do one of the following:
where first <> '1' or first is null
or
where coalesce(first, '<null>') <> '1'
In Oracle, null is not considered a legal value to select unless you explicitly ask for it:
select * from name where (first != '1') or first is null
You could also use NVL (similar to coalesce):
select * from name where nvl(first,'0') != '1'
That is correct because NULL can never be compared with anything else....
The only option that you have is to include a NULL check as an or in the command
SELECT * FROM NAME WHERE FIRST!=1 OR FIRST IS NULL
According to Oracle Documentation NULL is defined as a value not knownm or when the value is not meaningful. That is solely the reason why Oracle mentions not consider a value of ZERO as NULL. This is just an FYI, an addon. Thanks!
NULL is dumb. Period.
NULL is evil.
If X is NULL and Y is NULL, then X does in fact equal Y because they are both NULL.
It's also a PITA that I can't say
IF X IN ('a','B','C', null)
Because this condition happens. But now I have to say
IF ( X IN ('a','B','C') or X is NULL )
which is a waste of time and a risk of error if I forget the parentheses.
What irks me further is that NULL shouldn't happen in the first place. Fields (er... ok kids, I'll call them Columns) should always be initialized. Period. Stop the nulls. Don't allow them. Default values should always be zeroes or blanks so that those folks that are too lazy to initialize columns in their software will have them initialized for them automatically.
There are many instances where a failure to define default values of zeroes and blanks makes life more difficult than it has to be.