Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:30:22+00:00 2026-05-25T00:30:22+00:00

My MySQL database table has dates stored in the MySQL format (YYYY-MM-DD). I need

  • 0

My MySQL database table has dates stored in the MySQL format (YYYY-MM-DD). I need to construct an SQL query that will return rows with dates that either occur today or yesterday or a day before. For example, if the stored date is 1979-08-22, I would like this row returned if today is 8/22 (the same date in a different year) or 8/23 or 8/24.

I’m thinking of using the DAYOFYEAR() function, which returns a single value between 1 and 365 depending on the date. Here is the current query:

SELECT * FROM `theTable` WHERE DAYOFYEAR(CURDATE())-DAYOFYEAR(`theDate`) IN(0, 1, 2, -364, -363)

This query finds the difference in days between the current date and the stored date and if it’s 3 days, it should return the result. It seems to work.

Is this the right way to achieve the result I want or is there another, more efficient way? Is this likely to cause issues with leap years?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:30:23+00:00Added an answer on May 25, 2026 at 12:30 am
    SELECT * FROM `datesTable`
        WHERE (
            MONTH(`theDate`) = MONTH(CURDATE())
            AND DAY(`theDate`) = DAY(CURDATE())
        ) OR (
            MONTH(`theDate`) = MONTH(DATE_ADD(CURDATE(), INTERVAL -1 DAY))
            AND DAY(`theDate`) = DAY(DATE_ADD(CURDATE(), INTERVAL -1 DAY))
        ) OR (
            MONTH(`theDate`) = MONTH(DATE_ADD(CURDATE(), INTERVAL -2 DAY))
            AND DAY(`theDate`) = DAY(DATE_ADD(CURDATE(), INTERVAL -2 DAY))
        )
    

    Done!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I inherited MYSQL database that has lots of tables with data like CREATE TABLE
I have a user table in my mysql database that has a password column.
I have a mysql database with a table that has several fields including: enter_date
I am trying to query a table that has dates in it. I would
I have a following scenario in my SQL Server 2005 database. zipcodes table has
I have a table in my SQL database that stores an AccountID and an
I'm writing a report to get result from mysql database. The query has one
I have an ecommerce MySQL database that stores order information in the table 'orders'
I have 3 tables in a MySql Database: User; Company that has a date
I have a script that grabs data from a MySQL database table and creates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.