Michael A
Michael A

Reputation: 9900

What is this line saying / doing?

I've found the following line in a query and no matter how many ways I think about it I don't understand what it's intended to do... Could you please help me to understand why this would be in a where clause and how it reads in pseudo code?

      DATEADD(year,              
             DATEDIFF(year, Convert(datetime,upvHD.PropertyVal), GETDATE()),
             Convert(datetime,upvHD.PropertyVal) 
             ) > GETDATE()

Upvotes: 1

Views: 50

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239724

The expression

DATEADD(year,              
         DATEDIFF(year, Convert(datetime,upvHD.PropertyVal), GETDATE()),
         Convert(datetime,upvHD.PropertyVal) 
         )

Is taking whatever date is stored in PropertyVal and is resetting the year portion of that date to be the same as the current year.

This is then being compared to the current date. This might be used to check whether we've gone past the anniversary of a particular date.


To break it down, we first have this expression:

DATEDIFF(year, Convert(datetime,upvHD.PropertyVal), GETDATE())

Which computes the number of year transitions (new years eve -> new years day) which have occurred between PropertyVal and the current date. Then this expression:

DATEADD(year,              
         <previous>,
         Convert(datetime,upvHD.PropertyVal) 
         )

Is adding that same number of years onto PropertyVal.

Upvotes: 5

Related Questions