snippetsqlModerate
How do I find the next saturday in MySQL
Viewed 0 times
thehownextmysqlfindsaturday
Problem
I have trigger and a stored procedure in it (so SP runs when trigger runs). I need a function which finds the next saturday to put in SP.
So lets say, it is Wednesday (2015-7-22) today. If my trigger runs today, the SP in it must find the next Saturday (2015-7-25).
Plus, even if it is Saturday, but the time is earlier of 9.30 pm, it must find the current day also. After 9.30 pm, it must return the next Saturday.
I would like to the put my whole trigger and sp on here but I don't want to get here crowded. I just need ideas, thanks.
EDIT:
Thanks to oNaye, I coded that:
It worked now.
Solved.
So lets say, it is Wednesday (2015-7-22) today. If my trigger runs today, the SP in it must find the next Saturday (2015-7-25).
Plus, even if it is Saturday, but the time is earlier of 9.30 pm, it must find the current day also. After 9.30 pm, it must return the next Saturday.
I would like to the put my whole trigger and sp on here but I don't want to get here crowded. I just need ideas, thanks.
EDIT:
Thanks to oNaye, I coded that:
CREATE DEFINER=`root`@`localhost` PROCEDURE `newGuess`(
IN `muserID` INT,
IN `numm1` INT,
IN `numm2` INT,
IN `numm3` INT,
IN `numm4` INT,
IN `numm5` INT,
IN `numm6` INT)
begin
set @today = (select weekday(curdate())+1); /*monday is the first day in here*/
if @today=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY));
end if;
if @today = 6 then /* it is saturday */
set @current_time = (select curtime());
set @nextSaturday = (SELECT DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY));
if @current_time =CAST('21:30:00' AS time) then
set @nextSaturday = curdate()+INTERVAL 1 WEEK;
end if;
end if;
insert into guessesTBL (userID,num1,num2,num3,num4,num5,num6,current__datetime,draw_date) values (muserID,numm1,numm2,numm3,numm4,numm5,numm6,NOW(),@nextSaturday);
endIt worked now.
Solved.
Solution
You can use
Here what you have to do:
The query meaning:
With
Test:
EDIT:
If you need to calculate to get the next Saturday if the actual Saturday is at 9:30PM:
New syntax:
Today test:
But if we're on
WEEKDAY and DATE_ADD function to calculate the next weekday incoming.Here what you have to do:
SELECT
DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,
(6-WEEKDAY(NOW())),
(5-WEEKDAY(NOW()))) DAY);The query meaning:
With
DATE_ADD you will add an interval between the parameter NOW() and one condition assuming my weekday for Saturday is 5, you will have to evaluate if NOW() is greater than 5 or equal, you have to rest it with 6 (Sunday) and if NOW() is less than 5 you have to rest it 5 with the weekday of NOW().Test:
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2015-07-22 07:51:33 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY);
+------------------------------------------------------------------------------------------+
| DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY) |
+------------------------------------------------------------------------------------------+
| 2015-07-25 07:51:34 |
+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>EDIT:
If you need to calculate to get the next Saturday if the actual Saturday is at 9:30PM:
New syntax:
SET @ACTUAL_DATE=NOW();
SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
7,
IF(WEEKDAY(@ACTUAL_DATE)>=5,
(6-WEEKDAY(@ACTUAL_DATE)),
(5-WEEKDAY(@ACTUAL_DATE))))
DAY);Today test:
mysql> SET @ACTUAL_DATE=NOW();
Query OK, 0 rows affected (0.01 sec)
mysql> SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
-> 7,
-> IF(WEEKDAY(@ACTUAL_DATE)>=5,
-> (6-WEEKDAY(@ACTUAL_DATE)),
-> (5-WEEKDAY(@ACTUAL_DATE))))
-> DAY);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @NEXT_SATURDAY;
+---------------------+
| @NEXT_SATURDAY |
+---------------------+
| 2015-07-25 21:30:01 |
+---------------------+
1 row in set (0.00 sec)
mysql>But if we're on
July 25th at 21:30:01 (Saturday):mysql> SET @ACTUAL_DATE='2015-07-25 21:30:01';
Query OK, 0 rows affected (0.00 sec)
mysql> SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
-> 7,
-> IF(WEEKDAY(@ACTUAL_DATE)>=5,
-> (6-WEEKDAY(@ACTUAL_DATE)),
-> (5-WEEKDAY(@ACTUAL_DATE))))
-> DAY);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @NEXT_SATURDAY;
+---------------------+
| @NEXT_SATURDAY |
+---------------------+
| 2015-08-01 21:30:01 |
+---------------------+
1 row in set (0.00 sec)Code Snippets
SELECT
DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,
(6-WEEKDAY(NOW())),
(5-WEEKDAY(NOW()))) DAY);mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2015-07-22 07:51:33 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY);
+------------------------------------------------------------------------------------------+
| DATE_ADD(NOW(),INTERVAL IF(WEEKDAY(NOW())>=5,(6-WEEKDAY(NOW())),(5-WEEKDAY(NOW()))) DAY) |
+------------------------------------------------------------------------------------------+
| 2015-07-25 07:51:34 |
+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>SET @ACTUAL_DATE=NOW();
SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
7,
IF(WEEKDAY(@ACTUAL_DATE)>=5,
(6-WEEKDAY(@ACTUAL_DATE)),
(5-WEEKDAY(@ACTUAL_DATE))))
DAY);mysql> SET @ACTUAL_DATE=NOW();
Query OK, 0 rows affected (0.01 sec)
mysql> SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
-> 7,
-> IF(WEEKDAY(@ACTUAL_DATE)>=5,
-> (6-WEEKDAY(@ACTUAL_DATE)),
-> (5-WEEKDAY(@ACTUAL_DATE))))
-> DAY);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @NEXT_SATURDAY;
+---------------------+
| @NEXT_SATURDAY |
+---------------------+
| 2015-07-25 21:30:01 |
+---------------------+
1 row in set (0.00 sec)
mysql>mysql> SET @ACTUAL_DATE='2015-07-25 21:30:01';
Query OK, 0 rows affected (0.00 sec)
mysql> SET @NEXT_SATURDAY=DATE_ADD(@ACTUAL_DATE,INTERVAL IF(WEEKDAY(@ACTUAL_DATE)=5 && TIME(@ACTUAL_DATE)>'21:30:00',
-> 7,
-> IF(WEEKDAY(@ACTUAL_DATE)>=5,
-> (6-WEEKDAY(@ACTUAL_DATE)),
-> (5-WEEKDAY(@ACTUAL_DATE))))
-> DAY);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @NEXT_SATURDAY;
+---------------------+
| @NEXT_SATURDAY |
+---------------------+
| 2015-08-01 21:30:01 |
+---------------------+
1 row in set (0.00 sec)Context
StackExchange Database Administrators Q#107744, answer score: 10
Revisions (0)
No revisions yet.