Tuesday, January 24, 2017

SQL DATEADD() Function

It has been quite a while since I have provided a tip for you guys/gals, but now I am back on the wagon.   I will be posting on a regular basis.  I have also decided to stop numbering the tips from this day forward. That being said...

Yesterday I ran across a problem where I had to check to see if a certain file called 'Test' was received within the last two years.

To accomplish this, I used the DATEADD() Function which adds or subtracts a specific time interval from a given date.


Syntax:

DATEADD (datepart, number, date)

The datepart can be broken down by year (yy, yyyy) , quarter (qq, q) , month (mm, m), dayofyear (dy, y), day (dd, d), week (wk, ww), weekday (dw, w), hour (hh), minute (mi, n), and second (ss, s) just to name a few.





Example Code:

Select *  from Table where DocumentType = 'Test' 
and  DateReceived >= DATEADD (yy, -2, GETDATE())
If you like this post and want to see more, follow me on my website www.chadcompton.com Or if you prefer...