Solved

t-sql query add time to getdate function in where clause

Posted on 2014-12-11
10
152 Views
Last Modified: 2014-12-11
i'm using sql server 2008 R2.

I have a table with a smalldatetime column called FromDate

I want to filter FromDates that are between today's date and 3 days from today.

Today's date is 12/11/2014.

Right now my where clause looks like this:

WHERE ([FromDate] >= getdate()) AND ([FromDate] <= dateadd(days, 3, getdate()) )

But that doesn't take time into consideration.

how do I revise my where clause to to include 00:00:00 to today's date and 23:59:59 to 3 days from today.

So then my where clause would be filtering between

12/11/2014 00:00:00  (today's date)
and
 12/14/2014 23:59:59  (three days from day)
0
Comment
Question by:maqskywalker
  • 6
  • 3
10 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40495312
try

WHERE datediff(day, 0, [FromDate]) >= datediff(day, 0, getdate())) AND ([FromDate] <= datediff(day, 3, getdate())
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40495320
DateDiff returns an integer,days elapsed since Jan 1, 1900
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 250 total points
ID: 40495355
correction...

WHERE datediff(day, 0, [FromDate]) >= datediff(day, 0, getdate()) AND datediff(day, 0, [FromDate]) <= datediff(day, 3, getdate())
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 40495358
>12/11/2014 00:00:00  (today's date)  and  12/14/2014 23:59:59  (three days from day)
This can also be interpreted as >= 12/11/2014 and < 12/15/2014

SQL expert PortletPaul addresses this in his article Beware of Between.  An excellent read, and if it helped you click on the green 'Was this article helped you?' button at the end.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40495389
Hi,
I would address this a little differently.
Avoid use of functions on the data because this badly affects performance. (see: sargable ) i.e.

datediff(day, 0, [FromDate])

badly affects performance because indexes cannot be used and using that function can be removed.

All that is required is to set getdate() to the time 00:00:00 instead, that can be done in two ways:
cast(getdate() as date)      or
dateadd(day, datediff(day,0, getdate() ), 0)

so your where clause could be:

WHERE [FromDate] >= cast(getdate() as date) AND [FromDate]) < dateadd(day, 3, cast(getdate() as date) )
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40495399
BTW, earlier solution could be improved using BETWEEN

WHERE datediff(day, 0, [FromDate])  BETWEEN datediff(day, 0, getdate()) AND datediff(day, 3, getdate())

But performance-wise, as Paul stated, still will not be as good.

Mike
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40495418
Yikes: don't use between for date ranges... almost guaranteed to get you into trouble

Do take the time to read my article "Beware of Between" or
the best practice with date and time ranges is to avoid BETWEEN and to always use the form:

WHERE col >= '20120101' AND col < '20120201'
This form works with all types and all precisions, regardless of whether the time part is applicable.
http://sqlmag.com/t-sql/t-sql-best-practices-part-2
Itzik Ben-Gan
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40495450
Hi Paul,

I have promised myself to read the link "Beware of Between" thoroughly in a day or two because it will be very educational for me.

That said, DateDiff returns an integer so between int1 and int2 is what is at work. the values are no longer dates. I appreciate some comment on that for my later reference.

Regards,

Mike
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40495456
:)

Yes; between is OK for integers, but we return to my initial comment regarding performance of that approach.

My concern is that it is difficult for some to recognize that between is not appropriate for date ranges.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40495518
Thank you.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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