drupal 7 $query->where and DATE_FORMAT not recognized - mysql

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'");

Related

MySQL MONTH() doesn't return correct value

I'm trying to execute a query that gets the month from a date and it seems to give me an incorrect moth;
Code is:
MONTH(FROM_UNIXTIME(Datum))
And Datum is:
24/01/2017
The result should be 01 or 1 but instead it is 12 and I don't know why... If I don't try to get the moth of that date, it will give me 24/01/2017 so I'm not sure what's wrong...
Can anyone help me?
If your Datum is 24/01/2017, try following:
month(str_to_date('24/01/2017', '%d/%m/%Y'))
Use STR_TO_DATE() function to solve your problem.
SELECT MONTH(str_to_date('24/01/2017', '%d/%m/%Y'));

MySQL: using YEAR_MONTH

phpmyAdmin suggested an autocompleted function YEAR_MONTH() in the Query Editor that I can't find anywhere online. It would be awesome if this function existed.
Does it? If yes, how would one use it?
While it isn't a valid function, YEAR_MONTH is valid SQL. For instance:
SELECT EXTRACT(YEAR_MONTH FROM '2009-07-02 01:02:03');
See also the discussion at https://github.com/phpmyadmin/phpmyadmin/issues/11877
You can use:
SELECT YEAR('2015-09-30 11:19:00');
or
SELECT DATE_FORMAT('2015-09-30 11:19:00','%y:%c');
or
SELECT '2015-09-30 11:19:00' + INTERVAL '2:2' YEAR_MONTH;

IFNULL on query

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.

mysql query not returning results

I am executing this query
SELECT *
FROM temp
WHERE DATE_FORMAT(startTime,'%m/%d/%Y') = '7/15/2012'
and startTime column has this value '2012-07-15 12:00:00'
But this is not returning any results. Can somebody please help?
Change here:
7/15/2012
to:
07/15/2012
According to the documentation for the DATE_FORMAT function, %m is "Month, numeric (00..12)". Note the zero-padding. So you need to write '07/15/2012' rather than '7/15/2012'.
(And in case you're wondering — I have no idea what month #0 is. So far as I'm aware, the months range from 01 to 12. Maybe some locales do have a month #0?)

ADDTIME and SEC_TO_TIME to SQL Server 2008

I try to change my code from MYSQL to SQL Server.
I have some problems with specific functions (ADDTIME and SEC_TO_TIME) in my query.
Here is the end of my query which i have to change :
order by j.idjour,j.heure,ADDTIME(j.heure,SEC_TO_TIME(pl.duree)) asc
I tried to use convert and DateAdd but i am a bit disapointed about how to change it without any error.
Thanks for your help.
This should be what you are looking for,
dateadd(second, pl.duree, j.heure)
Assuming that pl.duree is an integer value representing seconds and that j.heure is a time or datetime value.