Error subtracting two dates

1

I am doing this function to subtract the date of an unpaid fine, from that date to the current date. But I get an error

DELIMITER $$
DROP FUNCTION IF EXISTS finesNotPayed $$
CREATE FUNCTION finesNotPayed (fineDate INTEGER)
RETURNS INTEGER
BEGIN
DECLARE i INTEGER ;
SET i=(SELECT DATEDIFF(CURRENT_DATE(),fineDate) FROM fine);
RETURN i;
END $$
DELIMITER ;



Error code 1292, incorrect datetime value
    
asked by kitkat 09.05.2018 в 22:39
source

1 answer

1

The two variables must be of type DATE for datediff to work. You can also use the NOW () function like this:

DELIMITER $$
DROP FUNCTION IF EXISTS finesNotPayed $$
CREATE FUNCTION finesNotPayed (fineDate DATE)
RETURNS INTEGER
BEGIN
DECLARE i INTEGER ;
SET i=(SELECT DATEDIFF(NOW(),fineDate) FROM fine);
RETURN i;
END $$
DELIMITER ;
    
answered by 09.05.2018 в 22:54