How to calculate date difference in mysql? - mysql

select sysdate-date('2016-12-02') from dual; --oracle
select now()-str_to_date('2016-12-02','%Y-%m-%d');-- mysql
Is there any way to get the day difference between two dates as number..??

You can use DATEDIFF with Strings or datefields, timestamps like this:
sample
SELECT DATEDIFF('2016-12-02 12:01:00', '2016_11_28 17:00:00') as cnt_days;
result
4

Another possible solution is:
SELECT TO_DAYS(date1)-TO_DAYS(date2) as cnt_days;
Hope it helps

Related

MySQL query to search in between two time range between two dates using timestamp data

I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)

Select in calendar with concat and between

I got a table called calendar, in that table I have this rows:
-day
-month
-year
why? Because I need it hehe.
So, the problem is when I want search a date in diferent year ( in the same year it's ok), for example:
Days between: 31-12-2013 and 1-1-2014, so I have a query:
SELECT * FROM calendar WHERE concat(year,'-',month,'-',day) BETWEEN '2013-1-30' AND '2013-1-31';
But, this query search in the same year and show the another months (I have in my db the 730 days) and not the 2014.
So, I'm confused because, how can I concat the rows and search with between?
Hope you can help me to understand, and sorry for my english.
Thanks for all.!!
Use str_to_date:
SELECT * FROM calendar
WHERE str_to_date(concat(year,'-',month,'-',day),'%Y-%m-%d')
BETWEEN '2013-1-30' AND '2013-1-31';
sqlfiddle demo
Try
SELECT * FROM calendar WHERE DATE(CONCAT(year,'-',month,'-',day)) BETWEEN '2013-1-30' AND '2013-1-31';
I'm also curious as to why you 'need' to store year, month and day as three seperate fields, and not in a single date field?
I think year, month and day may also be reserved keywords in mysql, so you may have to do
SELECT * FROM `calendar` WHERE DATE(CONCAT(`year`,'-',`month`,'-',`day`)) BETWEEN '2013-1-30' AND '2013-1-31';

Number of days between current date and date field

I have this problem if anyone can help.
There is a field (date) in my table (table1) that is a date in the format 3/31/1988 (M/D/y), and my necessity is to define how many days have passed since that date.
I have tried to give this instruction
SELECT DATEDIFF(CURDATE(), date) AS days
FROM table1
But it gives back 'null' and I think this happens because the two date formats are different (CURDATE() is YMD.....
Is it correct? can anyone help me?
Thank you in advance
You can use STR_TO_DATE():
SELECT DATEDIFF(CURDATE(),STR_TO_DATE(date, '%m/%d/%Y')) AS days
FROM table1
SQLFiddle Demo
Your DATE field should have DATE or DATETIME format to be used as DATEDIFF argument correctly.
Also DATE is MySQL keyword and I am not sure that you can use it as valid field name.
You can use this for accurate result
SELECT DATEDIFF(CURDATE(), DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(`date`)), '%Y-%m-%d')) AS days FROM `table1`
If you want to consider results without - signs that you have to follow parameters position as below :
SELECT DATEDIFF(Big_Date,Small_Date) AS days FROM table1.
positive results e.g 5 (with no sign), if you place a Small date as the first parameter then it will results minus sign e.g -5.

MySQL grouping by week, based on a date column?

I have a table with a date column and I would like to try and group by, using a week as a time reference in order to count how many rows occured per week. I have done this for days, using GROUP BY Date(Date_Column) but i'm unsure how to do this by week?
Thanks
SELECT ...
FROM ....
GROUP BY YEAR(Date_column), WEEKOFYEAR(Date_Column);
Try to put a GROUP BY YEARWEEK(date_column) at the end of your query - this will take in consideration also the year the date is in.
SELECT week(Date_Column)
FROM ....
GROUP BY week(Date_Column);
SELECT WEEKOFYEAR("2017-01-01"),YEARWEEK("2017-01-01"),WEEK("2017-01-01");
Outputs:
WEEKOFYEAR("2017-01-01") YEARWEEK("2017-01-01") WEEK("2017-01-01")
52 201701 1
Looks like YEARWEEK is the best solution. No need to concat the year.
SELECT CONCAT(YEAR(Date_Column),'/',WEEK(Date_Column)) AS efdt
FROM ....
GROUP BY efdt;

mySQL between dates that span over multiple years

Hi I was wondering why this statement is working in mySQL
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '02/21/2011' AND '03/20/2011'
but this is not
SELECT COUNT(*) AS `numrows`
FROM (`myTable`)
WHERE DATE_FORMAT(creationDateTime, '%m/%d/%Y') BETWEEN '12/21/2010' AND '03/20/2011'
The first statement returns 'xx' count of the number of rows while the second one returns '0'
The only difference I see is that the "from" date is in 2010 and the "end" date is in 2011. To test if this was the problem I queried from '12/31/2010' and it still gave me 0 results but when I set the start date as '01/01/2011' it gave me the number of records that were created in that time span.
Is there something I am missing with regards to mySQL's BETWEEN and using dates from different years?
Thanks for the help!
Try using the date format BETWEEN '2010-12-21' AND '2011-03-20'. Also remove the DATE_FORMAT() function.
So, this:
SELECT COUNT(*) AS `numrows`
FROM `myTable`
WHERE creationDateTime BETWEEN '2010-12-21' AND '2011-03-20'
DATE_FORMAT() returns a string, not a date. By using it you're forcing a string comparison instead of a date comparison. You should omit the DATE_FORMAT and use YYYY-MM-DD date strings instead.