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 8287577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:05:21+00:00 2026-06-08T12:05:21+00:00

I would like to select all the records in a table between two dates.

  • 0

I would like to select all the records in a table between two dates. I have a query like:

SELECT * FROM <table> WHERE (thedate BETWEEN DATE('2012-04-01') AND DATE('2012-06-30'))

This is fine for HSQL, Oracle, PostgreSQL, but it doesn’t work in SQL Server due to the lack of the Date function. Is there a way to get this to work generally for all databases (including SQL Server) or do I need to use an ORM like Hibernate (I know I should, but I’m specifically asking if this can be done in SQL)?

  • 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-06-08T12:05:22+00:00Added an answer on June 8, 2026 at 12:05 pm

    There’s no need for the Date(…) as far as i can tell. This example seems to work

    DECLARE @TheDate Date = '2012-07-01';
    
    SELECT 'hello' WHERE (@TheDate BETWEEN '2012-04-01' AND '2012-06-30')
    --None returned
    SET @TheDate = '2012-05-01'
    
    SELECT 'hello' WHERE (@TheDate BETWEEN '2012-04-01' AND '2012-06-30')
    --selects hello
    

    Edit Btw worth looking at This Question with the date time answer (will post here just to save effort)

    The between statement can cause issues with range boundaries for dates as

    BETWEEN '01/01/2009' AND '01/31/2009'
    

    is really interpreted as

    BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 00:00:00'
    

    so will miss anything that occurred during the day of Jan 31st. In this case, you will have to use:

    myDate >= '01/01/2009 00:00:00' AND myDate < '02/01/2009 00:00:00'  --CORRECT!
    

    or

    BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 23:59:59' --WRONG! (see update!)
    

    UPDATE: It is entirely possible to have records created within that last second of the day, with a datetime as late as 01/01/2009 23:59:59.997!!

    For this reason, the BETWEEN (firstday) AND (lastday 23:59:59) approach is not recommended.

    Use the myDate >= (firstday) AND myDate < (Lastday+1) approach instead.

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

Sidebar

Related Questions

i would like to select * from table where dates between (some_date and another_date)
I am trying to SELECT records from two MySql tables. I would like all
I'd like to select all records from a table ( names ) where lastname
I have this query: SELECT * FROM table WHERE key LIKE '1,2,3,%' OR key
I would like to select all elements that have certain attribute or don't have
I would like to select all matches from a commaseparated column in table2, where
I would like to delete all the rows found by that query: SELECT cart_abandon.*
I would like to run something like: select * from table where field in
I would like to select a row from the database based on two input
I want to select all records from a table Log where the DateAndTime field

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.