SQL Server Date Functions

SQL Server provides several date functions that allow you to work with date and time values. These functions help you retrieve, manipulate, and format dates and times in various ways. Here are some commonly used SQL Server date functions:

Returning the current date and time

FunctionDescription
CURREMT TIMESTAMPReturns the current system date and time without the time zone part
GETUTCDATEReturns date and UTC Time
GETDATEReturn the current system date and time of the operating system
SYSDATETIMEReturn the current system date and time with more fractional seconds precision than the GETDATE() function.
SYSUTCDATETIMEReturns the current system date and time in UTC time
SYSDATETIMEOFFSETReturns the current system date and time with the time zone.

Returning the current date and time Parts

FunctionDescription
DATENAMEReturns a date part of a date as a character string
DATEPARTReturns a date part of a date as an integer number
DAYReturns the day of a specified date as an integer
MONTHReturns the month of a specified date as an integer
YEARReturns the year of the date as  specified date as an integer

Returning a difference between two dates

FunctionReturn value
DATEDIFFReturns a difference in date part between two dates

Modifying dates

FunctionDescription
DATEADDAdds a value to a date part of a date and return the new date value.
EOMONTHReturns the last day of the month containing the specified date, with an optional offset
SWITCHOFFSETChanges the time zone offset of a DATETIMEOFFSET value and preserves the UTC value
TODATETIMEOFFSETTransforms a DATETIME2 value into a DATETIMEOFFSET value

Constructing date and time from their parts       

FunctionDescription
DATEFROMPARTSReturn a DATE value from the year, month, and day
DATETIME2FROMPARTSReturn a DATETIME2 value from the date and time arguments
DATETIMEOFFSETFROMPARTSReturn a DATETIMEOFFSET value from the date and time arguments
TIMEFROMPARTSReturns a TIME value from the time parts with the precisions

Validating date and time values

FunctionDescription
ISDATECheck if a value is a valid date, time, or datetime value

These are just a few examples of the date functions available in SQL Server. Each function has specific syntax and usage requirements, which can be found in the SQL Server documentation for the corresponding version of the software.

Leave a Reply

Your email address will not be published. Required fields are marked *