简体   繁体   中英

Date Difference Between SYSDATE and Date stored in MM-DD format

I am setting up a simple reminder system, and am using a MySQL table to store the dates.

The key things I need to store for the reminder date are the Day of the Month, and the Month.

However, the user has to pick the values from a DatePicker field, as taken from here: http://www.eyecon.ro/bootstrap-datepicker/

Therefore, if the user wanted a reminder date of 2nd September, the date is stored in aa "date" field the database in YYYY-MM-DD format as 2014-09-02.

The problem I am having is working out when that date is due within the next month.

Here is my sample table:

CREATE TABLE `test_table` (
  `fldID` int(11) NOT NULL AUTO_INCREMENT,
  `fldDate` date DEFAULT NULL,
  `fldDateVarChar` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`fldID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Some example data:

insert  into `test_table`(`fldID`,`fldDate`,`fldDateVarChar`) values (1,'2014-08-28','08-28');
insert  into `test_table`(`fldID`,`fldDate`,`fldDateVarChar`) values (2,'2016-10-09','10-09');
insert  into `test_table`(`fldID`,`fldDate`,`fldDateVarChar`) values (3,'2014-01-23','01-23');
insert  into `test_table`(`fldID`,`fldDate`,`fldDateVarChar`) values (4,'2015-09-18','09-18');

Sample Data:

fldID   fldDate     fldDateVarChar
------------------------------------------
1       2014-08-28  08-28
2       2016-10-09  10-09
3       2014-01-23  01-23
4       2015-09-18  09-18

Desired output:

fldID   TODAYS_DATE     CalcDate     Difference
--------------------------------------------------------------------
1       2014-08-23      2014-08-28   -5 
2       2014-08-23      2014-10-09   -47
3       2014-08-23      2014-01-23   212
4       2014-08-23      2014-09-18   -26

For the above, the 1st and 4th rows are relevant / useful in that the due dates fall within the next month.

The user picks a data using a date picker. I am only interested in the month and day. The year is not important.

I also store the MM-DD value in a VARCHAR field, just in case I need it.

I would like to work out an SQL statement to calculate the difference in days between SYSDATE (or NOW()) and the MM-DD part of the user's reminded date.

I cannot use eg DATEDIFF without also appending the year to the user's reminder date.

eg for a MM-DD value of "08-28" I would need something like:

SELECT DATEDIFF(NOW(),'2014-08-28') FROM DUAL;

But if I ran it in a years time, it would need to be:

SELECT DATEDIFF(NOW(),'2015-08-28') FROM DUAL;

That's what I'm having trouble with.

Can I achieve this in MySQL?

Maybe this works for you:

SELECT fldID,
 DATE_FORMAT(NOW(), '%Y-%m-%d') AS TODAYS_DATE,
 concat(year(now()),'-',fldDateVarChar) AS CalcDate,
 DATEDIFF(NOW(), concat(year(now()),'-',fldDateVarChar)) AS Difference
FROM test_table

Test it here: http://sqlfiddle.com/#!2/098372/5/0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM