IFNULL on query - mysql

In the query below, I would like to make the "Percent" value 0 when it is NULL. However, I can't seem to figure out how to wrap the IFNULL function into my query properly. Any help would be appreciated!
SELECT number, ((DATEDIFF (scheduled_start_date,gate_3_start_date) - DATEDIFF (scheduled_due_date,gate_4_delivery_date))*100 / (DATEDIFF (scheduled_due_date,gate_4_delivery_date))) as 'Percent' FROM dashboard

Use MySql's coalesce
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
it should work for you. I can provide an example if you need it.

Related

property of non-object to change value to 0

I have a curious question that's going on my mind while looking at my datatables.
I noticed that some columns of my table has no values, like just the column only.
Is it possible when you want to call that column it will return a value of 0?
I tried to call it with a simple SELECT query in sql but its giving me a notice.
Notice: Trying to get property of non-object MySQL result on line (number line)
Can this be used with an if-else statement from the query or a case statement from the query? Hoping for your opinions on this. Thank you :)
Try to use some function like NVL() in Oracle, or IFNULL() in MySQL. This functions set a value when you got a NULL value.

Access query amazing

When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;

SQL Case Statement

I have a table named as "allocation" with the columns named as "all_code", "grs_amt" and "cut_amt". I want to create a SQL statement to be got results as Gross_Value, Cut_Value and Net_Value. If the "cut_amt" IsNull then assign 0 for the Cut_Value and then calculate Net_Value using that assigned 0 value or available value in the "cut_amt". I used the following query.
SELECT allocation.grs_amt AS Gross_Value,
IfNull(allocation.cut_amt, 0) AS Cut_Value,
allocation.grs_amt - allocation.cut_amt AS Net_Value FROM
allocation
02) But unable to get the desired output. Can not understand what I am going wrong . Can anyone help me?
You have to repeat the expression:
SELECT a.grs_amt AS Gross_Value,
coalesce(a.cut_amt, 0) AS Cut_Value,
(a.grs_amt - coalesce(a.cut_amt, 0)) AS Net_Value
FROM allocation a;
I prefer coalesce() under most circumstances because it is ANSI standard.

IFERROR function in MySQL

Is there any function like IFERROR in MySQL. For example:
IFERROR(25/0,9)
I tried on phpmyadmin but it said that such function IFERROR does not exist. Really appreciate any help
YOu can reach similar results with IFNULL:
If an answer is not valid, it defaults to NULL. The IFNULL will then give you the alternative answer:
Your example (in this case):
SELECT IFNULL(25/0,9);
Gives as result: 9

drupal 7 $query->where and DATE_FORMAT not recognized

I have implement a function views_query_alter.
I want to add a where clause.
I try with : $query->add_where(0, "DATE_FORMAT(myfieldname, '%Y-%m-%d')", "DATE_FORMAT(now(), '%Y-%m-%d')", '>');
I also try with condition.
The result printed sql is the same from both cases and is DATE_FORMATmyfieldnameYmd. Is there any way to escaped a DATE_FORMAT or anything to succesfully recognized DATE_FORMAT function into where clause?
Thank you in advance
dont know if you found a solution but the following works for me
$query->add_where_expression(0, "DATE_FORMAT(STR_TO_DATE(myfieldname.value, '%Y-%m-%d'), '%Y-%m') > 'value'");