Date Functions
SQL Server provides a rich set of functions to work with dates and times. Use the table below to explore syntax, description, and examples.
Function | Description | Return Type | Example |
---|---|---|---|
GETDATE | Current database system timestamp. | datetime | SELECT GETDATE(); |
SYSDATETIME | Current system date and time with higher precision. | datetime2 | SELECT SYSDATETIME(); |
DATEADD | Adds a specified number to a date part of a date. | datetime | SELECT DATEADD(day, 7, GETDATE()); |
DATEDIFF | Returns the count of the specified date part boundaries crossed between two dates. | int | SELECT DATEDIFF(month, '2022-01-01', GETDATE()); |
DATENAME | Returns a character string representing the specified date part of the specified date. | nvarchar | SELECT DATENAME(weekday, GETDATE()); |
DATEPART | Returns an integer representing the specified date part of the specified date. | int | SELECT DATEPART(year, GETDATE()); |
CONVERT | Converts an expression of one data type to another. | varies | SELECT CONVERT(varchar, GETDATE(), 112); |
FORMAT | Formats a value with the given format and optional culture. | nvarchar | SELECT FORMAT(GETDATE(), 'yyyy-MM-dd'); |
EOMONTH | Returns the last day of the month that contains the specified date. | datetime | SELECT EOMONTH(GETDATE()); |
SWITCHOFFSET | Changes the time zone offset of a datetimeoffset value. | datetimeoffset | SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '+02:00'); |
TODATETIMEOFFSET | Converts a datetime2 value to datetimeoffset with a specified offset. | datetimeoffset | SELECT TODATETIMEOFFSET('2023-04-01 12:00:00', '-05:00'); |