extract year and month from date field mysql - mysql

How can I get year and month values only as yyyy-mm format from datetime field?
I tried
Select EXTRACT(YEAR_MONTH From task_completion) from task;
But this results in 201901 format.
Also I tried this solution from this post:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, task_completion), 0)
FROM task
Which gives Incorrect parameter count in the call to native function 'DATEDIFF' error, since this was solution for sql-server.
Is there any equivalent solution for mysql?

year and month function in mysql
select year(task_completion),month(task_completion) from task
but if you need year with month use DATE_FORMAT
select DATE_FORMAT(task_completion,'%Y-%m') from task

SELECT CONCAT(year(task_completion), '-' ,month(task_completion)) FROM task

Related

What is the MySQL Workbench version of DATEDIFF(YEAR,C.END_DT,GETDATE())?

I understand the syntax for the current date will change from GETDATE() to SYSDATE().
How do we write the difference between the date from c.end column and the current date in terms of years in MySQL?
Do we use TIMESTAMPDIFF() ?
There are many ways to get current year from MySQL, you just have to extract it using YEAR() function like this:
SELECT YEAR(NOW());
NOW() returns todays date + time in this format 'YYYY-MM-DD HH:MM:SS' so with YEAR(), you're just taking the year value from NOW(). Apply the same thing to the date field in your table like YEAR(C.END_DT) and do a subtraction between those year values. A simple query like this should work:
SELECT YEAR(NOW()) - YEAR(C.END_DT)
FROM mytable C;
But if you still want to use DATEDIFF, you can write something like this:
SELECT FROM_DAYS(DATEDIFF(NOW(),C.END_DT)) FROM mytable C;
DATEDIFF here return the differences in total days and using FROM_DAYS(), it will return the total year, month and day from the specific date in the comparison.
Refer to this updated fiddle

compare 2 dates, without considering the day

I need a SQL query to compare 2 dates based on their year and month only.
I.e. one date is 2017-07-12 and the second date is 2017-07-30.
Since they both share same month and year, I need to get them in my WHERE query.
I know how to do it with DATEFORMAT (converting both to the form of yyy-mm-01). I would like to know if there is a cleaner way.
You can compare their month and year using the Month() and Year() functions:
WHERE YEAR(date1)=YEAR(date2) AND MONTH(date1) = MONTH(date2);
Some good information on the mysql date and time functions page in their manual.
The MySQL function EXTRACT() can be used for this purpose:
WHERE EXTRACT(YEAR_MONTH FROM date1) = EXTRACT(YEAR_MONTH FROM date2)
EXTRACT(YEAR_MONTH FROM '2017-07-12') returns '201707'.

MYSQL - Retrieving weeks in a month

I'm trying to list all weeks in a month, (e.g., 2015-02-01 and 2015-02-28) in the following format: (week yyyy-mm-dd to week yyyy-mm-dd etc.)
Tried using WEEK e.g., SELECT WEEK(2015-02-01) - WEEK(2015-02-28); but this just gave me the number of numbers in the month - 4.
What is the proper MYSQL statement to achieve this?
DATE_FORMAT should work, have you tried it that way :
select date_format(your_date, '%Y-%m-%d')
from yourTable;
If your field is a string you could also use str_to_date
select str_to_date(your_date, '%Y-%m-%d')
from yourTable;

Cast function returning year and month only

select invoice_date, cast(invoice_date as date) as new_date from invoices limit 5;
I need to return only the month and year..any help?
I have to use the cast function
You can use the DATE_FORMAT to only show the month and year.
Select DATE_FORMAT(CAST(invoice_date as DATE), '%m/%Y') as new_date from invoices
You can use the MONTH() and YEAR() functions on the date type.
If you must use CAST(), then I guess you could cast to a string type and get a substring.
cast(invoice_date as char(7)) AS new_date
would give you 1999-10 for example.
I'm doing my homework with a similar question lol.

SQL extra only the day instead of entire date

I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers
Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table
you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );