In SMSS what's the difference between NULL and nothing, in a query result? - sql

I know this maybe a newbie question, but i'm wondering why I sometimes get NULL in a result, and sometimes it's just totally blank - shouldn't they all be null?

As you can see, the Remarks column has no background color of yellow which indicates that it is not NULL. (NULL values has.)
It contains an empty string '' (or maybe spaces) which is different from NULL because empty string is set as empty while NULL is not set (nothing).

In Oracle, the empty string is treated as NULL. SQL Server treats it as a string value.
The main difference is that in SQL Server you can return results from something like
select * from mytable where myfield = ''

Related

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

Standard Big query Not removing String

I'm trying to filter out "null" from a string column using Standard Big query and for whatever reason it is not being filtered out
so my where statement goes:
where d_transaction_dt <> "null"
d_transaction_dt is a string column that I'm trying to cast as date and remove anything in there that is "null"
I'm getting the error: Invalid date: 'null'
Please help..
Is it literally the string "null", or is the field null? If you're doing something like a SAFE_CAST to convert strings to a DATE type, you're probably getting NULL values.
WHERE d_transaction_dt <> "null"
is an entirely different filter predicate than
WHERE d_transaction_dt IS NOT NULL

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

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 = ' ')