sql check the select return null - mysql

I want to make an query can check completely null
for example:
select a where
case
when a like %b%
then a like %b% (if after search every row of table still return null)
else
a like %c% (if have match in any row skip else statement)

Sounds like you need the COALESCE() function:
SELECT COALESCE(a, b, c, ..., 'default')
this'll return the first NON-null value in the argument list, e.g.
COALESCE(null, null, 'hello')
returns hello.

Related

What does a zero value coalesce function do?

I have been exploring dbt tools and I came across the following code snippet :
coalesce(customer_orders.number_of_orders, 0) as number_of_orders
I understand that a coalesce function is used to return the first non-null value in a list. What I do not understand is what does the zero in the second parameter signify?
The COALESCE function returns the first non-null value in a list. COALESCE can take n number of arguments.
COALESCE(val1, val2, ...., val_n)
So according to the query:
coalesce(customer_orders.number_of_orders, 0) as number_of_orders
In case customer_orders.number_of_orders is NULL the result returned in number_of_orders would be 0.
COALESCE can use as many arguments as you want. In most cases (like in your example), COALESCE(some_column,0) is used to prevent that creating a sum or building an average will not lead to the desired result.
Assume there are three columns and you want to sum them. In case you don't use COALESCE, the sum will be NULLeven if only one of your three columns is NULL. So you will use COALESCEand replace NULL values by zero in order to receive the sum of all NOT NULL values.
You can "translate" COALESCE into a CASE WHEN construct:
COALESCE(column1,0)
does following:
CASE WHEN column1 IS NULL THEN 0 ELSE column1 END
Another use case of COALESCE is to replace column1 by column2 if column1 is NULL, if also column2 is NULL, take column3 etc.
I created an example here, so you can see what I mean:
db<>fiddle

Select field conditionally from mysql

Is it possible to use Select with Case to select from one of several fields depending on which of them is null or not?
I basically want to return a value for all records with logic that says
Return $Value from FIeldA if not null
else from FieldB if not NULL
else from FieldC if not Null
else '0'
I've used Case/When/Then to compare values from a specific field but not as a way of comparing values between fields and not sure if this is possible.
Short answer, use COALESCE:
SELECT COALESCE(FieldA, FieldB, FieldC, 0) AS FieldName
FROM tableName;
It will give you the first non nullable value from the three fields FieldA, FieldB, FieldC. If all are null, then it will return 0. Which what you are trying to do.
Long answer, use CASE expression.
if you want to check multiple filed and if null then return same result 0 then use COALESCE function . this is simple code
SELECT COALESCE(filed1, filed2, filed3, 0) as output from table;
for more information
https://www.w3schools.com/sql/func_sqlserver_coalesce.asp
extra option if you want to select filed using condition then use case. this is demo code
SELECT CASE 1 WHEN 1 THEN 'this is case one'
WHEN 2 THEN 'this is case two'
ELSE 'this is not in the case'
END as 'how to execute case statement';
for more information
https://dev.mysql.com/doc/refman/5.7/en/case.html
http://www.mysqltutorial.org/mysql-case-function/

MySQL return case value

I'm looking for a way to return the value that was "case"-ed on.
e.g. what will return me the md5 result without recomputing it at the then statement
SELECT
CASE md5(col1,col2,col3,...,coln)
WHEN MD5('') then NULL
else ???
end
If you do not want to repeat a calculation multiple times in the select list, then you need to push it into a subquery and reference the field alias set in the subquery:
SELECT
CASE t1.calc
WHEN MD5('') then NULL
else t1.calc
end as md_5
FROM
(select md5(col1,col2,col3,...,coln) as calc from table) t1

MySQL COALESCE with two arguments?

COALESCE is an SQL function that returns the first non-NULL expression among its arguments. So in the following statement...
SELECT USER.user_id,
USER.firstname,
USER.lastname,
...
COALESCE(EMPLOYEE.title, '') title,
...
FROM USER
... it is basically saying that if EMPLOYEE.title is NULL, then return and use '' instead. Is my understanding correct?
Let's say that EMPLOYEE.title equals 'CEO'. If we plug this into the COALESCE function, our query would look something like:
SELECT COALESCE('CEO', '') sub_sector;
If we ran it, we would get 'CEO'. Now let's say that EMPLOYEE.title is NULL. If we plug that into the COALESCE function, our query would look something like:
SELECT COALESCE(NULL, '') sub_sector;
If we run that, we will get '' since COALESCE returns the first non-null value in its argument list. Since the first value is NULL, it will then check the next value, '', which is not NULL, so it will return it.
In the case of your query, if the field EMPLOYEE.title has a NULL value, the COALESCE function will return ''.

MySQL comparison with null value

I have a column called CODE in a MySQL table which can be NULL. Say I have some rows with CODE='C' which I want to ignore in my select result set. I can have either CODE=NULL or CODE!='C' in my result set.
The following query does not return a row with CODE as NULL:
SELECT * from TABLE where CODE!='C'
But this query works as expected and I know it is the right way to do it.
SELECT * from TABLE where CODE IS NULL OR CODE!='C'
My question is why does having only CODE!='C' does not return rows where CODE=NULL? Definitely 'C' is not NULL. We are comparing no value to a character here. Can someone throw some light as why it doesn't work that way?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.
Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.
Any arithmetic comparison with 'NULL' will return false. To check this in SQL:
SELECT IF(NULL=123,'true','false')
To check NULL values we need to use IS NULL & IS NOT NULL operator.
Based on my tests and the documentation here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You can compare null and get a boolean result using <=>
NOTE: it looks like NOT EQ operator, but it's EQ operator
For example:
select x <=> y;
or
select #x <=> #y;
This also compares string vs null, string vs string, etc.
In SQL, the NULL value is a special value, not comparable with any other one.
The result of a direct comparison with a NULL is always NULL, although (unfortunately) you may find FALSE in some implementation.
To test a null value you should use IS NULL and IS NOT NULL.
SELECT *
FROM `table_name`
WHERE IFNULL(`column_name` != 'C', TRUE)
The specified problem can also appear in joins and the above answers aren't particularly helpful. The way I prefer to do it is by coalescing to otherwise impossible value. For example, this
select foo from bar
inner join baz on bar.x = baz.y
won't work if bar.x and baz.y are both nulls (join won't bring results). The workaround is to use e.g.
select foo from bar
inner join baz on coalesce(bar.x, -1) = coalesce(baz.y, -1)
where -1 is "impossible" value meaning it can never appear in the data set.
select * from user where application_id='1223333344' and name is null;
I use:
SELECT * from TABLE where NOT(CODE <=> 'C')