MySQL fails to add days to a date when month is over - mysql

I am trying to get a final date when a number of days (pplazo input) has elapsed, starting on a date (pfecha input), and avoiding to count certain dates (feriado) that are listed on a table.
So far this is what i got to, using a Stored Procedure:
Input parameters:
pfecha -- DATE
pplazo -- INT (11)
PROC:BEGIN
DECLARE i INT(1);
START TRANSACTION;
SET i=1;
lazo:LOOP
IF NOT EXISTS (SELECT * FROM feriados WHERE feriado=pfecha+i)
THEN
SET pfecha=pfecha+1;
SET i=i+1;
END IF;
IF i=pplazo
THEN
LEAVE lazo;
END IF;
END LOOP lazo;
COMMIT;
SELECT pfecha as respuesta;
END
The thing is, when the days to count make the date go pass the end of the month, then the "respuesta" turns to 0000-00-00.
This shouldn't be happening, if I input 15 days starting on the 2016-04-20 then the resulting date should be something like 2016-05-5.
Can you spot my mistake? Could you point me in the right direction?

The correct way to add a number of days to a date is with the DATE_ADD or ADDATE functions, not the + operator. See
lazo:LOOP
IF NOT EXISTS (SELECT * FROM feriados WHERE feriado=DATE_ADD(pfecha, INTERVAL i DAY))
THEN
SET pfecha=DATE_ADD(pfecha, INTERVAL 1 DAY);
SET i=i+1;
END IF;
IF i=pplazo
THEN
LEAVE lazo;
END IF;
END LOOP lazo;

Related

Create a loop based on date Mysql

I have a query :
insert into fookoo_business
select stat_date, sum(spend), sum(revenue)
from hooloo_business;
that i want to run for each date from '2017-01-20' until yesterday (it means the query will run 434 times if we're at 01/04/2018), for each date separately
(in a loop).
how can i create a loop in Mysql to do it for me?
I have tried:
creating procedure for the query select #stat_date, sum(spend), sum(revenue)
I called 'query'
then :
CREATE PROCEDURE loop_procedure()
BEGIN
SET #stat_date='2018-03-20';
CALL 'query';
REPEAT
SET #stat_date = #stat_date + INTERVAL 1 DAY;
UNTIL #stat_date = CURDATE() END REPEAT;
END
eventually i've used the following logic within a stored procedure to fetch the data:
PROCEDURE `x_monitoring_loop`()
BEGIN
DECLARE i INT;
DECLARE len INT;
SET len = 434;
SET i = 0;
WHILE (i < len) DO
SET #stat_date= CURDATE()-INTERVAL 1 DAY;
SET #stat_date= #stat_date- INTERVAL i DAY;
Insert query;
SET i = i +1;
END WHILE;
This way the query ran 434 times for each day, beginning at current date - 1 day.
I do not know why you want to use a procedure,I think we can just use a query sql to do it:
INSERT INTO fookoo_business
SELECT stat_date, SUM(spend), SUM(revenue)
FROM hooloo_business
WHERE stat_date BETWEEN STR_TO_DATE('2017-01-02', '%Y-%m-%d') -- start date
AND DATE_SUB(NOW(), INTERVAL 1 DAY) -- end date
GROUP BY stat_date;

Stored procedure MySQL 5.5.16

So I have in my database a table called "weeks" where I store every weeks of the years like so:
table weeks(id, year, num_week, date_min, date_max)
So for this week, the line look like this :
Note : My weeks starts on thursday and ends on Wenesday.
Since it is a pain inserting each weeks line by line, I want to create a stored procedure for this, here's what I came up with :
DELIMITER |
CREATE PROCEDURE proc_insert_weeks()
BEGIN
SELECT year, num_week, date_min, date_max INTO #year, #num_week, #date_min, #date_max
FROM weeks
ORDER BY date_min DESC LIMIT 1;
SET #date_min = DATE_ADD(#date_max INTERVAL 1 DAY);
SET #date_max = DATE_ADD(#date_min INTERVAL 6 DAY);
SET #year= YEAR(#date_min);
IF #num_week < 52 THEN SET #num_week = #num_week + 1;
ELSE SET #num_week = 1;
END IF;
INSERT INTO weeks (year, num_week, date_min, date_max)
VALUES (#year, #num_week, #date_min, #date_max);
END |
DELIMITER ;
So the idea was to take the last record of the table and add 1 week to the dates, but I can't even make it paste the creation of the procedure.
I get an error right after the SELECT query, can someone help me figure out what I am doing wrong ?
drop PROCEDURE proc_insert_weeks;
DELIMITER |
CREATE PROCEDURE proc_insert_weeks()
BEGIN
SELECT year, num_week, date_min, date_max INTO #year, #num_week, #date_min, #date_max
FROM weeks
ORDER BY date_min DESC LIMIT 1;
SET #date_min = DATE_ADD(#date_max, INTERVAL 1 DAY);
SET #date_max = DATE_ADD(#date_min, INTERVAL 6 DAY);
SET #year= YEAR(#date_min);
IF #num_week < 52 THEN SET #num_week = #num_week + 1;
ELSE SET #num_week = 1;
END IF;
INSERT INTO weeks (year, num_week, date_min, date_max)
VALUES (#year, #num_week, #date_min, #date_max);
END |
DELIMITER ;
You can try above code.
You made mistake in DATE_ADD function. You missed , in it.

SQL: DATE_ADD(date,INTERVAL expr type) skip weekends

I'm currently using DATE_ADD(date,INTERVAL expr type) to set a due date as a trigger in a mySQL Database.
What I'm wanting to know is if it is possible to skip weekends (Saturday, Sunday) as part of the trigger.
You'd have to create an own function for doing that. You can look how to do that in this answer, for example (just use function instead of procedure). As for how to write such a function, here's a working algorithm. The code is quite straightforward: it loops through days and skips weekends.
CREATE FUNCTION `DAYSADDNOWK`(addDate DATE, numDays INT) RETURNS date
BEGIN
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
WHILE numDays>0 DO
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
SET numDays=numDays-1;
END WHILE;
RETURN addDate;
END
Currently SELECT DAYSADDNOWK(CURDATE(), 5) yields 2016-03-07, which is correct.
Of course you only can use it with days, so no arbitrary interval, but your question mentioned date datatype, and I don't quite see how one could add a month not counting working days.
This function simply creates a list of dates starting at the date given in the arguments, and then figures out which date is x number of days (the interval) out while disregarding days 1 and 7 (which are Sunday and Saturday respectively on SQL Server).
CREATE FUNCTION [dbo].[udf_days_add_no_wknd]
(
#start_date date
, #interval int
)
RETURNS date
AS
BEGIN
declare #answer date
; with dates as
(
select #start_date as date_val
union all
select dateadd(d, 1, date_val) as date_val
from dates
where date_val < dateadd(d, #interval * 10, #start_date)
)
, final as
(
select top 1 lead(ld.date_val, #interval, NULL) over (order by ld.date_val asc) as new_date_val
from dates as ld
where 1=1
and datepart(dw, ld.date_val) not in (1,7) --eliminating weekends
)
select #answer = (select new_date_val from final)
return #answer
END
It is worth nothing that this solution is dependent on having SQL Server 2012 or later, considering the use of the lead() function.

Scope of mysql while loop

I'm having some trouble with scope using mysql while loops.
DECLARE vdate DATETIME DEFAULT now();
DECLARE vday int default 0;
WHILE (vday < 7) DO
WHILE (vdate < DATE_ADD(NOW(),INTERVAL 1 DAY)) DO
//Here vday is always 0
END WHILE;
//Here vday is 0 -6
SELECT vday +1 INTO vday;
END WHILE;
Can anyone clarify for me what the scope of a while loop is in mysql? How to I create a variable i can use inside a nested loop?
Turns out I missed something obvious (late friday problem :) ). I wasn't setting vdate back to now() after each loop of vday. As a result it would only get into the sub loop when vday was 0.

Whats wrong with this short procedure (MySQL)

Im trying to make a procedure, which will be checking if user is already logged (he got a session, and im checking if his last action was over 15 minutes ago). My procedure looks like this:
CREATE PROCEDURE `isLogged`(in p_sessid VARCHAR(32), out res INT(1))
BEGIN
DECLARE v_customer_id INT(9);
DECLARE v_date DATE;
SELECT customer_id INTO v_customer_id FROM Sessions WHERE sessid=p_sessid;
SELECT expiry_date INTO v_date FROM Sessions WHERE sessid=p_sessid;
SET res=3;
IF v_customer_id > 0 THEN
IF UNIX_TIMESTAMP(NOW()) > UNIX_TIMESTAMP(v_date) THEN
DELETE FROM Sessions WHERE sessid=p_sessid;
SET res=1;
ELSE
UPDATE Sessions SET expiry_date=DATE_ADD(NOW(), INTERVAL 15 MINUTE) WHERE customer_id=v_customer_id;
SET res=0;
END IF;
END IF;
END
Can anyone tell, why it always return 1, what means that user is not logged anymore? I were checking manually expression UNIX_TIMESTAMP(NOW()) > UNIX_TIMESTAMP(v_date), and it gives me 0 in response, so? Whats going on?
Thanks in advance,
Marcin
The first IF statement should read like this:
UNIX_TIMESTAMP(NOW()) > UNIX_TIMESTAMP(DATE_ADD(v_date, INTERVAL 15 MINUTE))
or else NOW will always be greater than the last login date.
You may rewrite/optimize your procedure to function. For example -
CREATE FUNCTION `isLogged`(IN p_sessid VARCHAR(32))
RETURNS INT
BEGIN
DELETE FROM Sessions WHERE sessid = p_sessid AND v_date <= NOW() - INTERVAL 15 MINUTE;
IF ROW_COUNT() > 0 THEN -- check how many rows were deleted
RETURN 1;
ELSE
UPDATE Sessions SET expiry_date = NOW() + INTERVAL 15 MINUTE WHERE customer_id = v_customer_id;
IF ROW_COUNT() > 0 THEN -- check how many rows were updated
RETURN 0;
END IF;
END IF;
RETURN 3;
END
Also, you can try to debug your code to understand the error.
Omg, that was very stupid.
There was a type mismatch. v_date type was a DATE, and this is just a day! Like 2011-12-14.
Solution:
change DATE -> DATETIME.
And now everything works good.
Anyway, thank you for answers.