Using Between to select a date range does not retrieve inclusive dates.

Posted on 2006-07-16
Last Modified: 2012-10-02
Hi there

I always thought that “BETWEEN”  Select * from table where SEARCH_DATE between '2005-12-01' AND '2005-12-31');
would select rows from say "2005-12-01 00:00:00" to "2005-12-31 23:59:59", seems like MYSQL does not.

I have a table. . .

3878879|2005-12-01 00:00:14
3878880|2005-12-01 00:00:15
. . .
3900061|2005-12-31 23:59:58
3900062|2005-12-31 23:59:58

When I do a select

FROM table1
SEARCH_DATE between '2005-12-01' AND '2005-12-31');

. . .I only get rows up until '2005-12-30 23:59:59'

If I do
FROM table1
SEARCH_DATE between '2005-12-01' AND '2005-12-31 23:59:59');

I do get the exact result

Is this to be expected?

How can I format the input date to search up to 23:59:59?

kind regards,
Question by:vandersk
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 19

Expert Comment

by:Kim Ryan
ID: 17119777
BETWEEN is the same as saying x >= min_val and x <= max_val

I think that specifyig only '2005-12-31'. mysql will assume a time value of 0:00:00

Alternatively, You could say this
SEARCH_DATE between '2005-12-01' AND '2006-01-01');
LVL 35

Accepted Solution

Raynard7 earned 250 total points
ID: 17120237
You should also be able to do date(SEARCH_DATE) between '2005-12-01' AND '2005-12-31'

As date converts all datestimes to dates ie  to 0:00:00
LVL 35

Expert Comment

ID: 17452578
I do not understand why this answer merits a C - if you required more information - why didnt you ask for it?

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question