Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Select records using between...

Posted on 2014-04-01
Medium Priority
Last Modified: 2014-04-30

Have two dates that i use in this simple example:

select * from TransactionTable
where CreatedDate between '2013-12-17' and '2013-12-19'

But the problem is that it dosent give me records from 2013-12-19
Only up to -> 2013-12-19

How can i fix this
Question by:team2005
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
LVL 29

Accepted Solution

sammySeltzer earned 1500 total points
ID: 39969589
Maybe, you can try CASTING the compare date:

select * from TransactionTable
where  CAST(CreatedDate as datetime) between '2013/4/4' and '2014/4/4'

Open in new window

LVL 70

Expert Comment

by:Scott Pletcher
ID: 39969798
The foolproof way to do that, no matter what the column data type is, is:

where CreatedDate >= '2013-12-17' and
            CreatedDate < '2013-12-20'

Btw, you definitely don't want to use a function on the table column, as that will prevent index seeks from occurring for that column (in technical terms, a function call makes it "NONSARGABLE").
LVL 32

Expert Comment

ID: 39970285
The problem is likely that you have a time element in your dates. When selecting '2013-12-19' it assumes a time of 00:00:00 or 12 am on the 19th. So any CreatedDate with a time other than midnight on the 19th will not be inclusive in your between clause. The easiest way to get around that is to use the >= and < as ScottPletcher has suggested.
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.


Author Comment

ID: 39985316

where CreatedDate >= '2013-12-17' and
            CreatedDate <= '2013-12-20'

Dosent include 2013-12-20 ?
LVL 32

Expert Comment

ID: 39985890
It would only include midnight on 2013-12-20 and not any createdDate with a time value of greater than or equal to 00:00:01.

Author Closing Comment

ID: 40031375

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

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