Solved

SQL Date with minimum time and like date with maximum time

Posted on 2013-06-19
4
277 Views
Last Modified: 2013-06-19
If I enter dates into a parameter and both dates are the same how can I make one of the dates the minimum value for that date so 00:00 and the end date 23:59.

so
BETWEEN @StartDate AND @EndDate

I know its likely to be simple but I just cant figure it.

Regards
S
0
Comment
Question by:SimonJohnG
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39259503
don't use between

use this instead

[field] >= @StartDate AND [field] < dateadd(day,1,@EndDate)

so, if the parameters are both '2013-06-01'

this equates to:
[field] >= '2013-06-01' AND [field] < dateadd(day,1,'2013-06-01') -- i.e. '2013-06-02'

If the parameters are '2013-06-01' & '2013-06-30'

this equates to:
[field] >= '2013-06-01' AND [field] < dateadd(day,1,'2013-06-30') -- i.e. '2013-07-01'

avoiding "between" is the solution.
for more on this topic please see: "Beware of Between"
0
 

Author Comment

by:SimonJohnG
ID: 39259572
Thank you for your help, works great.

Regards
Simon
0
 

Author Closing Comment

by:SimonJohnG
ID: 39259580
Fantastic reponse time and clear and precise explaination. Thank you
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39261034
and thank you! Quick closures is very much appreciated too.

Do take be cautious of using 'between' when filtering by date ranges, the >= with < construct is usually a more reliable method (You just need to get used to the idea that the higher date is 'the next day'). Do read that article if you need more.

Cheers, Paul
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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