9 April 2014

Add and Subtract Dates using DATEADD in SQL Server

Below are examples of using the DATEADD function.  
For all of these examples, the parameter @Date = "2011-09-23 15:48:39.2370000".

Examples of Using the DATEADD Function
Unit of timeQueryResult
NANOSECONDSELECT DATEADD(NANOSECOND,150000,@Date)2011-09-23 15:48:39.2371500
MICROSECONDSELECT DATEADD(MICROSECOND,150000,@Date)2011-09-23 15:48:39.3870000
MILLISECONDSELECT DATEADD(MILLISECOND,1500,@Date)2011-09-23 15:48:39.2385000
SECONDSELECT DATEADD(SECOND,1500,@Date)2011-09-23 16:13:39
MINUTESELECT DATEADD(MINUTE,15,@Date)2011-09-23 16:03:39
HOURSELECT DATEADD(HOUR,-3,@Date)2011-09-23 12:48:39
WEEKDAYSELECT DATEADD(WEEKDAY,-2,@Date)2011-09-21 15:48:39
WEEKSELECT DATEADD(WEEK,4,@Date)2011-10-21 15:48:39
DAYSELECT DATEADD(DD,4,@Date)2011-09-27 15:48:39
DAYOFYEARSELECT DATEADD(DY,14,@Date)2011-10-07 15:48:39
MONTHSELECT DATEADD(MM,-4,@Date)2011-05-23 15:48:39
QUARTERSELECT DATEADD(Q,-2,@Date)2011-03-23 15:48:39
YEARSELECT DATEADD(YYYY,-5,@Date)2006-09-23 15:48:39


No comments:

Post a Comment