Solved

no records are output when start and end date are same

Posted on 2014-03-19
7
562 Views
Last Modified: 2014-03-19
Hi,

I have a table with three columns namely

id int
name varchar(40)
create_date datetime  -> data ex. 2014-03-15 15:00:09.923

I write the query below to get records within a particular date,everthhing works fine except when the start date and end date are same ,no result is displayed.
Query
select id,name from test where create_date >='2012/12/12' and create_date<='2012/12/12'

It return no record,however in the database record exists.
what modification need to get above result.
0
Comment
Question by:Rocking
7 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 39940156
you have a datetime column but you aren't specifying a time.  Try something like what I have below.

select id,name from test where create_date >= '2012/12/12 00:0:01' and create_date <= '2012/12/12 23:59:59'
0
 

Author Comment

by:Rocking
ID: 39940166
i think there is no issue of time

how come it returns the record if there is a gap between a single day,
select id,name from test where create_date >='2012/12/12' and create_date<='2012/12/13'

above works fine
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 200 total points
ID: 39940183
Most likely , the column you are trying to filter on [Create_Date] also contains a time value.  You may not be able to see it because of the way the field is formatted, either in the table design or in a query, but if you eliminate any format property from the field it will probably display with a data and time.

Generally, when working with dates, I write my queries like:

WHERE [Create_Date] >= [StartDate]
AND [Create_Date] < [StartDate] + 1

This way, you will capture all records created starting at midnight on the [StartDate] and running up to, but not including midnight of the next night.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 200 total points
ID: 39940184
>i think there is no issue of time
You are not correct.

>where create_date >='2012/12/12'
Without a time component, as dunstock stated above, SQL will interpret it as the stroke of midnight that begins the day, i.e. '2012-12-12 00:00:00.000'

Therefore, your SQL is the same as saying..
select id,name from test where create_date >='2012-12-12 00:00:00.000' and create_date<='2012-12-12 00:00:00.000'.

'2012-12-12 00:00:00.000' will meet the above criteria, as it equals both.
'2014-12-12 15:00:09.923' does not, as it is greater than both.

SQL expert PortletPaul has an article on Beware of Between that does a great job of illistrating your situation.
0
 

Author Comment

by:Rocking
ID: 39940217
is the below query correct to get the desired result

select id,name from test where create_date >='2012/12/21' and
create_date < DATEADD(dd,1,'2012/12/21')
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39940375
Yes, that should work.
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 100 total points
ID: 39940523
In general, but it's best to write it using literals, like this:

where create_date >='20121221' and
    create_date < 20121222'

Otherwise you might hurt performance and/or have unexpected side effects if you change the data type of the column, for example, to datetime2.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now