Datetime Functions: SQL Server
SQL Server 2008 introduces a set of high-precision functions to return current datetime information:
SYSDATETIME()
Returns date and time as a DATETIME2 value.
SYSDATETIMEOFFSET()
Returns date, time, and time zone offset as a DATETIMEOFFSET value.
SYSUTCDATETIME
Returns current UTC time as a DATETIME2 value.
SQL Server continues to support the following functions from previous releases:
CURRENT_TIMESTAMP
orGETDATE()
Returns the current date and time on the server as a datetime value.
GETUTCDATE()
Returns the current UTC date and time, as derived from the server’s time and time zone setting.
SQL Server implements two functions for date arithmetic:
DATEADD(
datepart
,interval
,
date
)
Adds
interval
(expressed as an integer) todate
. Specify a negative interval to perform subtraction. Thedatepart
argument is a keyword specifying the portion of thedate
to increment, and it may be any of the following: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
,milli
second
,ms
. For example, to add one day to the current date, useDATEADD(day, 1, GETDATE())
.DATEDIFF(
datepart
,
startdate
,
enddate
)
Returns
enddate
–startdate
expressed in terms of the units you specify for thedatepart
argument. For example, to compute the number of minutes between the current time and UTC time, useDATEDIFF(mi, GETUTCDATE(),
GETDATE())
.
SQL Server 2008 introduces new functions to work with time zone offsets:
SWITCHOFFSET( ...
Get SQL Pocket Guide, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.