Skip to main content

SQL DATEADD Function




The DATEADD function is used to add an interval to a date in MS SQL SERVER
The usage for the DATEADD function is
DATEADD (datepart, number, expression)
where the data type of <expression> is some type of date, time, or datetime. <number> is an integer (can be positive or negative). <datepart> can be one of the following:
datepart
abbreviation
year
yy, yyyy
quarter
qq, q
month
mm, m
dayofyear
dy, y
day
dd, d
week
wk, ww
hour
hh
minute
mi, n
second
ss, s
millisecond
ms
microsecond
mcs
nanosecond
ns
TZoffset
tz
ISO_WEEK
isowk, isoww

The result returned has the same data type as <expression>.


SQL DATEADD Syntax
DATEADD ( datepart , number, date )

Query:SELECT DATEADD(Year, 3, '2012-06-04')
OUTPUT Value = 2015-06-04 00:00:00.000

Query:
SELECT DATEADD(Month, 3, '2012-06-04')
OUTPUT Value = 2012-09-04 00:00:00.000

Query:SELECT DATEADD(dayofyear,3, '2012-06-04')
OUTPUT Value = 2012-06-07 00:00:00.000

Query:
SELECT DATEADD(Day, 3, '2012-06-04')
OUTPUT Value = 2012-06-07 00:00:00.000

Query:SELECT DATEADD(Week, 3, @DateNow)
OUTPUT Value = 2012-06-25 00:00:00.000

Query:SELECT DATEADD(Hour, 3, @DateNow)
OUTPUT Value = 2012-06-04 03:00:00.000

Query:
SELECT DATEADD(minute, 3, @DateNow)
OUTPUT Value = 2012-06-04 00:03:00.000

Query:SELECT DATEADD(second, 3, @DateNow
OUTPUT Value = 2012-06-04 00:00:03.000

Query:SELECT DATEADD(millisecond, 3, @DateNow)
OUTPUT Value = 2012-06-04 00:00:00.003

Returns a new datetime value based on adding an interval to the specified date.

Popular posts from this blog

Dot Net FrameWork

The .NET Framework has two main components: the 1)Common Language Runtime (CLR) and the 2).NET Framework class library. The .NET Framework provides a Runtime environment called the Common Language Runtime or (CLR) that handles the execution of the code and provides useful services for the implementation of the application. CLR: The Runtime can be considered an agent that manages code at execution time. Thus providing core services such as memory management, thread management, and remoting. Also incorporating strict type safety, security and robustness. The Common Language Runtime (CLR) is the virtual machine component of the .NET framework. All .NET programs execute under the supervision of the CLR, guaranteeing certain properties and behaviors in the areas of memory management, security, and exception handling. Class Library: The class library is a comprehensive collection of reusable types that you can use to develop traditional command-line, WinForm (graphical user interface) appli...

SQL Difference between Union & Union All

SQL Difference between Union & Union All The union and union all operators allow you to combine multiple data sets. The difference between the two is that union sorts the combined set and removes duplicates while union all does not. With union all, the number of rows in the final data set will always equal the sum of the number of rows in the sets being combined.[Learning SQL By Alan Beaulieu] When using the UNION command all selected columns need to be of the same data type. For example : X Y UNION UNION ALL A B A A A B B A B A - B - - - B - - - B - - - A Union all is faster than union, union's duplicate elimination requires a sorting operation, which takes time.

Change date format in jquery datepicker

Change date format in jquery datepicker We can change the date format in jquery ui datepicker. <script> $(function() { $("#datepicker1").datepicker({showOn: 'both', buttonImage: 'images/calendar.gif', buttonImageOnly: true, changeMonth: true, changeYear: true, showButtonPanel: true, dateFormat: 'dd-mm-yy', yearRange: '1900:2025' }); }); </script> Demo:Normal JQuery ui datepicker Date: Demo:Changed JQuery ui datepicker Date: