skip to Main Content

If I have dates Column and i want to select dates of monday and sunday only from past 1 year, how can i do it with effective code in SQL

I tried to select manually the dates but its very long process so can i get a proper method for the same

3

Answers


  1. How about a combination of date range checking and DAYOFWEEK()?

    SELECT *
    FROM mytable
    WHERE mydatecolumn BETWEEN (CURDATE() - INTERVAL 1 YEAR) AND CURDATE()
    AND DAYOFWEEK(mydatecolumn) IN (1, 2)
    
    Login or Signup to reply.
  2. You can select dates that fall on Mondays and Sundays from the past year in MySQL using a query with the DATE and DAYOFWEEK functions.

    SELECT date_column
      FROM your_table_name
     WHERE date_column BETWEEN CURDATE() - INTERVAL 1 YEAR AND CURDATE()  -- Select dates from the past year
       AND DAYOFWEEK(date_column) IN (1, 2);      -- Filter for Mondays (2) and Sundays (1)
    
    Login or Signup to reply.
  3. SELECT *
    FROM have
    WHERE DAYOFWEEK(date) IN (1, 2)
      AND date BETWEEN CURDATE() - INTERVAL 1 YEAR AND CURDATE()
    ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search