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.
SYSUTCDATETIMEReturns current UTC time as a DATETIME2 value.
SQL Server continues to support the following functions from previous releases:
CURRENT_TIMESTAMPorGETDATE()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. Thedatepartargument is a keyword specifying the portion of thedateto 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,millisecond,ms. For example, to add one day to the current date, useDATEADD(day, 1, GETDATE()).DATEDIFF(datepart,startdate,enddate)Returns
enddate–startdateexpressed in terms of the units you specify for thedatepartargument. 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( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access