Cast function returning year and month only - mysql

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.

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

extract year and month from date field 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

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'.

How to convert a date string to mysql date format for calculating date diff using mysql query

I have a column where a date store in ddmmyy format (e.g. 151216). How can I convert it to yyyy-mm-dd format (e.g 2016-12-15) for calculating a date difference from the current date? I try using DATE_FORMAT function but its not appropriate for this.
If you want to get the date difference, you can use to_days() after converting the string to a date using str_to_date():
select to_days(curdate()) - to_days(str_to_date(col, '%d%m%y'))
or datediff():
select datediff(curdate(), str_to_date(col, '%d%m%y'))
or timestampdiff():
select timestampdiff(day, str_to_date(col, '%d%m%y'), curdate())
You can use the function, STR_TO_DATE() for this.
STR_TO_DATE('151216', '%d%m%y')
A query would look something like:
select
foo.bar
from
foo
where
STR_TO_DATE(foo.baz, '%d%m%y') < CURDATE()
Note: Since both STR_TO_DATE() and CURDATE() return date objects, there's no reason to change the actual display format of the date. For this function, we just need to format it. If you wanted to display it in your query, you could use something like
DATE_FORMAT(STR_TO_DATE(foo.baz, '%d%m%y'), '%Y-%m-%d')
To get the difference, we can simply subtract time
select
to_days(CURDATE() - STR_TO_DATE(foo.baz, '%d%m%y')) as diff
from
foo
If you wanted to only select rows that have a difference of a specified amount, you can put the whole to_days(...) bit in your where clause.
SELECT STR_TO_DATE('151216', '%d%m%y') FROM `table`
use this '%d%m%y'

How to make SQL query elapse time between selected date and now?

Let say i have a date field and i want to calculate the days between
Selected date on the date field and now() as in system date how do i make the query using SQL?
select DATEDIFF(columnwithdatename, NOW()) from table
Use the DateDiff function:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff
e.g.
SELECT DATEDIFF(MyDateColumn, NOW()) FROM MyTable;
use the sql datediff function
for example
select datediff(day, myDate, getdate()) as daysAgo
from myTable
Maybe try using DATEDIFF SQL function (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff).
So for example:
SELECT DATEDIFF(NOW(), your_date_field) FROM your_table;