Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate a third monday of every month using SQL statement?

Tags:

mysql

i need to trigger a notification. this notification has to be triggered every third monday of every month.

like image 346
user1145402 Avatar asked Jan 12 '12 11:01

user1145402


People also ask

How can calculate days in a month in SQL?

In SQL Server 2012 and later version, we can get the total number of days in a month using EOMONTH() function. This method is the easiest way to get the total number of days in a month.

How do I get Monday of every week in SQL?

MySQL WEEKDAY() Function The WEEKDAY() function returns the weekday number for a given date. Note: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

How do I get the first Friday of the month in SQL?

fetch the first day of the every month then check for its day (as in.. monday,sunday etc..) then on the basis of the outcome .. add 0-6 days to the firstday of the month .. this should give you the expected result set.


2 Answers

SELECT 
(
  DAYOFWEEK(NOW()) = 2  
  AND 
  DAYOFMONTH(NOW()) BETWEEN 15 AND 21
) 
AS send_notice_today;
like image 90
Tom Haws Avatar answered Nov 15 '22 00:11

Tom Haws


Try using dayofweek and dayofmonth functions. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek

Somehow you can check how many weeks are there from 1st of month to curdate() with dayofmonth (using an operation mod 7), and dayofweek should be 5 (thursday)

like image 42
de3 Avatar answered Nov 14 '22 22:11

de3