4

I did not find any of examples for MySQL - all of them were quite complicated.

How can I SELECT the first Sunday of the month?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Aitvaras
  • 261
  • 4
  • 15
  • one more on SO : http://stackoverflow.com/questions/3317980/getting-first-day-of-the-week-in-mysql-using-week-no – xkeshav Nov 16 '12 at 09:26

2 Answers2

8

So choose the first day of the month: 2012-01-01 (or whatever month and year you want).

Get the weekday index of the date. Indexes here are from 0 to 6.

Subtract that index from 6 and you will get how many days you need to add until the date is Sunday.

Add that amount of days to the chosen day.

SELECT DATE_ADD("2012-01-01 10:00:00", INTERVAL (6 - WEEKDAY("2012-01-01 10:00:00")) DAY);

Or:

SELECT DATE_ADD("2012-01-01", INTERVAL (6 - WEEKDAY("2012-01-01")) DAY);
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Aitvaras
  • 261
  • 4
  • 15
0

So choose the first day of the month: 2021-08-01 (or whatever month and year you want).

SELECT ADDDATE( '2021-08-01' , MOD((8-DAYOFWEEK('2021-08-01')),7))

  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 31 '21 at 08:36
  • Yes, right provide more details as other answer for this question. For that it will more helpfull for others – Rajeev Singh Aug 31 '21 at 18:22