Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What's the difference between these two "qualifiers?"

Posted on 2016-11-11
3
Medium Priority
?
83 Views
Last Modified: 2016-12-01
What's the difference between these two qualifiers:

datediff(d, max(t.created), getDate()) <= 45

and...

max(txn.created) > DATEADD(DAY, -45, GETDATE())

I'm thinking with the first one you have all of the rows where the t.created value falls within a range defined by the difference of today's date and whatever t.created values equals to 45 days or less.

The second clause is all of the rows that have a date that's greater than today's date minus 45 days.

Even when I attempt to explain that, I'm thinking they are fundamentally the same, but am I missing something?
0
Comment
Question by:brucegust
[X]
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
3 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 41884260
datediff(d, max(t.created), getDate()) <= 45

= The number of calendar days (not necessarily 24-hour days) between t.created and the current date must be <= 45.


max(txn.created) > DATEADD(DAY, -45, GETDATE())

= The date and time in txn.created must be greater than the date and time 45 days before today.


The time factor is probably the most significant difference between them.
0
 
LVL 32

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 500 total points
ID: 41884421
Both are same if you consider the logic.

This <<datediff(d, max(t.created), getDate()) <= 45>> is really bad for performance as it will execute this expression for each row in the table and will lead to scanning the entire table even if you have index on the column.

This <<max(txn.created) > DATEADD(DAY, -45, GETDATE()) >> is good, since we are doing maths with the constant value and SQL will calculate the constant separately since it is not dependent on the any table.

Hope it helps !!
0
 
LVL 80

Assisted Solution

by:arnold
arnold earned 500 total points
ID: 41884665
the material difference is the logical comparison, one deals with integer comparison, the other performs datetime comparison.
The datetime comparison, can be simplified by assigning the date 45 days ago into a variable to avoid running dateadd for every row
Declare @date as datetime
Set @date=dateadd(d,-45,getdate())

Max(txt.created)>@date

Question is the cost of running datediff on every row of a result set less or more than datetime comparison, if not mistaken, the comparison on datetime also scans the entire result set datetime fields are not indexed........
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

636 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