SQL DATEADD

The SQL DATEADD() function is used to add or subtract a time interval to a date.

The DATEADD() function is supported in SQL Server. In MySQL, it has similar function SQL ADDDATE().

SQL DATEADD Syntax

DATEADD(datepart,number,date-expression)
datepart Abbreviation
year yyyy,yy
quarter qq,q
month mm,m
dayofyear dy,y
day dd,d
week ww,wk
weekday dw,w
hour hh
minute mi,n
millisecond ms
microsecond mcs
nanosecond ns

SQL DATEADD Examples

Get next year’s date:

SELECT DATEADD(yyyy,1,GETDATE()) AS [Next Year]

The result will look like:

Next Year
2012-09-04 21:21:42.320

Get the date after 90 days:

SELECT DATEADD(dd,90,GETDATE()) AS [90 Days]

The result will look like:

90 Days
2011-12-03 21:23:20.260

Get the date 4 weeks before:

SELECT DATEADD(ww,-4,GETDATE()) AS [4 Weeks Before]

The result will look like:

4 Weeks Before
2011-08-07 21:24:55.773

Get the date 12 hours before:

SELECT DATEADD(hh,-12,GETDATE()) AS [12 Hours Before]

The result will look like:

12 Hours Before
2011-09-04 09:26:17.413

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>