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

  • Home
  • SEARCH
  • 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 197495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:51:28+00:00 2026-05-11T16:51:28+00:00

Suppose the following table structure: Event: id: integer start_date: datetime end_date: datetime Is there

  • 0

Suppose the following table structure:

Event: 
  id: integer
  start_date: datetime
  end_date: datetime

Is there a way to query all of the events that fall on a particular day of the week? For example, I would like to find a query that would find every event that falls on a Monday. Figuring out if the start_date or end_date falls on a Monday, but I’m not sure how to find out for the dates between.

Pure SQL is preferred since there is a bias against stored procedures here, and we’re calling this from a Rails context which from what I understand does not handle stored procedures as well.

  • 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-11T16:51:28+00:00Added an answer on May 11, 2026 at 4:51 pm
    SELECT  *
    FROM    event
    WHERE   EXISTS
            (
            SELECT  1
            FROM    dual
            WHERE   MOD(start_date - TO_DATE(1, 'J') + level - 1, 7) = 6
            CONNECT BY
                    level <= end_date - start_date + 1
            )
    

    The subquery iterates all days from start_date to end_date, checks each day, and if it’s a Monday, returns 1.

    You can easily extend this query for more complex conditions: check whether an event falls on ANY Monday OR Friday 13th, for instance:

    SELECT  *
    FROM    event
    WHERE   EXISTS  (
            SELECT  1
            FROM    dual
            WHERE   MOD(start_date - TO_DATE(1, 'J') + level - 1, 7) = 6
                    OR (MOD(start_date - TO_DATE(1, 'J') + level - 1, 7) = 3 AND TO_CHAR(start_date + level - 1, 'DD') = '13')
            CONNECT BY
                    level <= end_date - start_date + 1
            )
    

    Note that I use MOD(start_date - TO_DATE(1, 'J') + level - 1, 7) instead of TO_CHAR('D'). This is because TO_CHAR('D') is affected by NLS_TERRITORY and should not be used for checking for a certain day of week.

    This query does not use any indexes and always performs a full table scan. But this is not an issue in this specific case, as it’s highly probable that a given interval will contain a Monday.

    Even if the intervals are 1 day long, the index will return 14% of values, if intervals are longer, even more.

    Since INDEX SCAN would be inefficient in this case, and the inner subquery is very fast (it uses in-memory FAST DUAL access method), this, I think, will be an optimal method, both by efficiency and extensibility.

    See the entry in my blog for more detail:

    • Checking event dates
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is also very easy to do with SSIS data… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer Where are my Gems? You can find where your gems… May 11, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer There are two new "special" operators in this F# release,… May 11, 2026 at 7:03 pm

Related Questions

This is something that comes up so often I almost stopped thinking about it
Suppose the following data schema: Usage ====== client_id resource type amount Billing ====== client_id
I have a very specific html table construct that seems to reveal a Gecko
I have a Products table in a SQL Server database and I am having
Is this how one would normally design classes? One class = 1 Table. How

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.