MySQL: using YEAR_MONTH - mysql

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;

Related

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

How to format time to '2:34 hrs', '0:34 hrs' etc in MySQL

I am writing a query that is selecting two times and calculating the difference between these, where one is the start and the other is the finish to find out the duration. However both are set to TIME types in mysql. How can i format the time instead of showing HH:MM:SS to '[OPTIONAL H]H':MM hrs'
Do i use the extract function?
I presume i have to wrap the TIMEDIFF around something else?
my sql code is as follows:
SELECT operator_no, log_in_time, TIMEDIFF(log_in_time,log_out_time) AS Duration
many thanks
Yes, wrap it in TIME_FORMAT():
SELECT operator_no, log_in_time,
TIME_FORMAT(
TIMEDIFF(log_in_time, log_out_time),
'%H:%i hrs'
) AS Duration
would this not work too?
SELECT operator_no, log_in_time,
CONCAT(EXTRACT(HOUR_MINUTE FROM TIMEDIFF (log_in_time,end_time)) + 'hrs' )
AS Duration

MySQL phpMyAdmin error in a simple date query

In MySQL using phpMyAdmin I am trying out this simple query to fetch rows that satisfy a certain date criteria:
select *
from student_invoice
where date_generated < '2012-01-01'
The date_generated is of date type. I get an error in phpMyAdmin that says:
ERROR: Unclosed quote # 64 STR: '
I have closed all quotes so its not making sense. The phpMyAdmin version is 2.11.9.6
Adding a new answer, as it's unrelated to my other one.
According to this bugzilla post here, your version suffers from this bug!
Upgrading to 2.11.11 or higher should fix this issue.
This may sound silly, but have you tried wrapping the date in double quotes?
SELECT *
FROM sometable
WHERE somedatecolumn < "2012-01-01"
Make sure that those are actually single quotes surrounding the date, not backticks.
Not familiar with the specific error. But you could try casting your static date to a date format, just to make sure it jives with the datecolumn format. Or even casting both? I.e:
where cast(somedatecolumn as DATE) < cast('2012-01-01' as DATE)
I have a feeling that won't work though. So maybe this?:
where somedatecolumn < cast('2012-01-01' as DATE)

How can I resolve this issue using an sql query

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in Layman's expression ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
Remove the quotes around the field names
SELECT
DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) AS cap_maturity FROM capital
I think this should work.
I know its is an old question, but just trying to help others

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.