DATEADD (datepart , number , date )
DatePart | Abbreviations |
year | yy, yyyy |
quarter | qq, q |
month | mm, m |
dy, y | |
day | dd, d |
week | wk, ww |
dw, w | |
hour | hh |
minute | mi, n |
second | ss, s |
ms | |
mcs | |
ns |
-- Add Days To A Date
SELECT DATEADD(DD, 10, '2020-05-01') AS 'ADD 10 Days To A Date'
-- Output ==> 2020-05-11 00:00:00.000
SELECT DATEADD(DD, 30, '2020-05-01') AS 'ADD 30 Days To A Date'
-- Output ==> 2020-05-31 00:00:00.000
SELECT DATEADD(d, 10, '2020-05-01') AS 'ADD 10 Days To A Date'
-- Output ==> 2020-05-11 00:00:00.000
SELECT DATEADD(DAY, 50, '2020-05-01') AS 'ADD 50 Days To A Date'
-- Output ==> 2020-06-20 00:00:00.000
-----------------------------------------------------------------
-- Subtract Days to A Date
SELECT DATEADD(DD, -10, '2020-05-01') AS 'Subtract 10 Days To A Date'
-- Output ==> 2020-04-21 00:00:00.000
SELECT DATEADD(DD, -30, '2020-05-01') AS 'Subtract 30 Days To A Date'
-- Output ==> 2020-04-01 00:00:00.000
SELECT DATEADD(d, -10, '2020-05-01') AS 'Subtract 10 Days To A Date'
-- Output ==> 2020-04-21 00:00:00.000
SELECT DATEADD(DAY, -50, '2020-05-01') AS 'Subtract 50 Days To A Date'
-- Output ==> 2020-03-12 00:00:00.000
-- ADD Quarter To A Date
SELECT DATEADD(QQ, 2, '2020-05-01') AS 'ADD Quarter To A Date'
-- Output ==> 2020-11-01 00:00:00.000
SELECT DATEADD(Q, 2, '2020-05-01') AS 'ADD Quarter To A Date'
-- Output ==> 2020-11-01 00:00:00.000
SELECT DATEADD(QUARTER, 2, '2020-05-01') AS 'ADD Quarter To A Date'
-- Output ==> 2020-11-01 00:00:00.000
--------------------------------------------------------------
-- Subtract Quarter To A Date
SELECT DATEADD(QQ, -2, '2020-05-01') AS 'Subtract Quarter To A Date'
-- Output ==> 2019-11-01 00:00:00.000
SELECT DATEADD(Q, -2, '2020-05-01') AS 'Subtract Quarter To A Date'
-- Output ==> 2019-11-01 00:00:00.000
SELECT DATEADD(QUARTER, -2, '2020-05-01') AS 'Subtract Quarter To A Date'
-- Output ==> 2019-11-01 00:00:00.000
-- ADD Month To A Date
SELECT DATEADD(MM, 6, '2020-05-01') AS 'ADD Month To A Date'
-- Output ==> 2020-11-01 00:00:00.000
SELECT DATEADD(M, 6, '2020-05-01') AS 'ADD Month To A Date'
-- Output ==> 2020-11-01 00:00:00.000
SELECT DATEADD(MONTH, 6, '2020-05-01') AS 'ADD Month To A Date'
-- Output ==> 2020-11-01 00:00:00.000
--------------------------------------------------------------
-- Subtract Month To A Date
SELECT DATEADD(MM, -6, '2020-05-01') AS 'Subtract Month To A Date'
-- Output ==> 2019-11-01 00:00:00.000
SELECT DATEADD(M, -6, '2020-05-01') AS 'Subtract Month To A Date'
-- Output ==> 2019-11-01 00:00:00.000
SELECT DATEADD(MONTH, -6, '2020-05-01') AS 'Subtract Month To A Date'
-- Output ==> 2019-11-01 00:00:00.000
-- ADD Year To A Date
SELECT DATEADD(YYYY, 5, '2020-05-01') AS 'ADD Year To A Date'
-- Output ==> 2025-05-01 00:00:00.000
SELECT DATEADD(YY, 5, '2020-05-01') AS 'ADD Year To A Date'
-- Output ==> 2025-05-01 00:00:00.000
SELECT DATEADD(YEAR, 5, '2020-05-01') AS 'ADD Year To A Date'
-- Output ==> 2025-05-01 00:00:00.000
--------------------------------------------------------------
-- Subtract Year To A Date
SELECT DATEADD(YYYY, -5, '2020-05-01') AS 'Subtract Year To A Date'
-- Output ==> 2015-05-01 00:00:00.000
SELECT DATEADD(YY, -5, '2020-05-01') AS 'Subtract Year To A Date'
-- Output ==> 2015-05-01 00:00:00.000
SELECT DATEADD(YEAR, -5, '2020-05-01') AS 'Subtract Year To A Date'
-- Output ==> 2015-05-01 00:00:00.000
-- ADD Weeks To A Date
SELECT DATEADD(WK, 1, '2020-05-01') AS 'ADD Weeks To A Date'
-- Output ==> 2020-05-08 00:00:00.000
SELECT DATEADD(WW, 1, '2020-05-01') AS 'ADD Weeks To A Date'
-- Output ==> 2020-05-08 00:00:00.000
SELECT DATEADD(WEEK, 1, '2020-05-01') AS 'ADD Weeks To A Date'
-- Output ==> 2020-05-08 00:00:00.000
--------------------------------------------------------------
-- Subtract Weeks To A Date
SELECT DATEADD(WK, -1, '2020-05-01') AS 'Subtract Weeks To A Date'
-- Output ==> 2020-04-24 00:00:00.000
SELECT DATEADD(WW, -1, '2020-05-01') AS 'Subtract Weeks To A Date'
-- Output ==> 2020-04-24 00:00:00.000
SELECT DATEADD(WEEK, -1, '2020-05-01') AS 'Subtract Weeks To A Date'
-- Output ==> 2020-04-24 00:00:00.000
-- ADD DayofYear To A Date
SELECT DATEADD(DY, 5, '2020-05-01') AS 'ADD DayofYear To A Date'
-- Output ==> 2020-05-06 00:00:00.000
SELECT DATEADD(Y, 5, '2020-05-01') AS 'ADD DayofYear To A Date'
-- Output ==> 2020-05-06 00:00:00.000
SELECT DATEADD(DAYOFYEAR, 5, '2020-05-01') AS 'ADD DayofYear To A Date'
-- Output ==> 2020-05-06 00:00:00.000
--------------------------------------------------------------
-- Subtract DayofYear To A Date
SELECT DATEADD(DY, -5, '2020-05-01') AS 'Subtract DayofYear From A Date'
-- Output ==> 2020-04-26 00:00:00.000
SELECT DATEADD(Y, -5, '2020-05-01') AS 'Subtract DayofYear From A Date'
-- Output ==> 2020-04-26 00:00:00.000
SELECT DATEADD(DAYOFYEAR, -5, '2020-05-01') AS 'Subtract DayofYear From A Date'
-- Output ==> 2020-04-26 00:00:00.000
-- ADD WeekDay To A Date
SELECT DATEADD(DW, 5, '2020-05-01') AS 'ADD WeekDay To A Date'
-- Output ==> 2020-05-06 00:00:00.000
SELECT DATEADD(W, 5, '2020-05-01') AS 'ADD WeekDay To A Date'
-- Output ==> 2020-05-06 00:00:00.000
SELECT DATEADD(WEEKDAY, 5, '2020-05-01') AS 'ADD WeekDay To A Date'
-- Output ==> 2020-05-06 00:00:00.000
--------------------------------------------------------------
-- Subtract WeekDay To A Date
SELECT DATEADD(DW, -5, '2020-05-01') AS 'Subtract WeekDay To A Date'
-- Output ==> 2020-04-26 00:00:00.000
SELECT DATEADD(W, -5, '2020-05-01') AS 'Subtract WeekDay To A Date'
-- Output ==> 2020-04-26 00:00:00.000
SELECT DATEADD(WEEKDAY, -5, '2020-05-01') AS 'Subtract WeekDay To A Date'
-- Output ==> 2020-04-26 00:00:00.000
-- Declare a Date With Time
DECLARE @Date DATETIME2 = '2020-05-20 17:47:12.2730000'
-- ADD Time Units To A Date
-- Add Hours to A DateTime
SELECT DATEADD(HH, 1, @Date) AS 'ADD HOURS To A Date'
-- Output ==> 2020-05-20 18:47:12.2730000
-- Add Minutes to A DateTime
SELECT DATEADD(MI, 10, @Date) AS 'ADD Minutes To A Date'
-- Output ==> 2020-05-20 17:57:12.2730000
-- Add Seconds to A DateTime
SELECT DATEADD(SS, 1500, @Date) AS 'ADD Seconds To A Date'
-- Output ==> 2020-05-20 18:12:12.2730000
-- Add MilliSeconds to A DateTime
SELECT DATEADD(MS, 1500, @Date) AS 'ADD MilliSeconds To A Date'
-- Output ==> 2020-05-20 17:47:10.7730000
-- Add MicroSeconds to A DateTime
SELECT DATEADD(MCS, 150000, @Date) AS 'ADD MicroSeconds To A Date'
-- Output ==> 2020-05-20 17:47:12.4230000
-- Add Nano Seconds to A DateTime
SELECT DATEADD(NS, 150000, @Date) AS 'ADD Nano Seconds To A Date'
-- Output ==> 2020-05-20 17:47:12.2731500
--------------------------------------------------------------
-- Subtract Time Units To A Date
-- Subtract Hours to A DateTime
SELECT DATEADD(HH, -1, @Date) AS 'Subtract Hours To A Date'
-- Output ==> 2020-05-20 16:47:12.2730000
-- Subtract Minutes to A DateTime
SELECT DATEADD(MI, -10, @Date) AS 'Subtract Minutes To A Date'
-- Output ==> 2020-05-20 17:37:12.2730000
-- Subtract Seconds to A DateTime
SELECT DATEADD(SS, -1500, @Date) AS 'Subtract Seconds To A Date'
-- Output ==> 2020-05-20 17:22:12.2730000
-- Subtract MilliSeconds to A DateTime
SELECT DATEADD(MS, -1500, @Date) AS 'Subtract MilliSeconds To A Date'
-- Output ==> 2020-05-20 17:47:10.7730000
-- Subtract MicroSeconds to A DateTime
SELECT DATEADD(MCS, -150000, @Date) AS 'Subtract Microseconds To A Date'
-- Output ==> 2020-05-20 17:47:12.1230000
-- Subtract Nano Seconds to A DateTime
SELECT DATEADD(NS, -150000, @Date) AS 'Subtract Nana Seconds To A Date'
-- Output ==> 2020-05-20 17:47:12.2728500
Comments