In this article, we will learn how to use the Date Functions in SQL.
SQL Server has many built-in Date Functions.
Function | Description |
---|---|
CURRENT_TIMESTAMP() | It returns the current date and time.
SELECT CURRENT_TIMESTAMP AS CurrentDateTime; Output: 2019-12-10 12:14:29.110 |
DATEADD() | It adds a date/time interval to date and then returns the date.
SELECT DATEADD(MONTH, 1, '2019/12/10') AS AddDate; Output: 2020-01-10 00:00:00.000 |
DATEDIFF() | It returns the difference between two dates.
SELECT DATEDIFF(DAY, '2019/12/10', '2020/01/01') AS DiffDate; Output: 22 |
DATEFROMPARTS() | It returns a date from the specified parts (year, month, and day).
SELECT DATEFROMPARTS(2019, 12, 10) AS DateFromParts; Output: 2019-12-10 |
DATENAME() | It returns a specified part of a date in the string.
SELECT DATENAME(MONTH, '2019/12/10') AS DatePartString; Output: December |
DATEPART() | It returns a specified part of a date in integer.
SELECT DATEPART(MONTH, '2019/12/10') AS DatePartInt; Output: 12 |
DAY() | It returns the day of the month for a specified date.
SELECT DAY('2019/12/10') AS DayOfMonth; Output: 10 |
GETDATE() | It returns the current database system date and time.
SELECT GETDATE() AS CurrentDateTime; Output: 2019-12-10 12:14:29.110 |
GETUTCDATE() | It returns the current database system UTC date and time.
SELECT GETUTCDATE() AS CurrentUTCDateTime; Output: 2019-12-10 06:55:54.407 |
ISDATE() | It checks an expression and returns 1 if it is a valid date, otherwise 0.
SELECT ISDATE('2019-12-32'); Output: 0 |
MONTH() | It returns the month part for a specified date.
SELECT MONTH('2019/12/10') AS Month; Output: 12 |
SYSDATETIME() | It returns the date and time of the SQL Server.
SELECT SYSDATETIME() AS CurrentSysDateTime; Output: 2019-12-10 12:28:37.3831871 |
YEAR() | It returns the year part for a specified date.
SELECT YEAR('2019/12/10') AS Year; Output: 2019 |
Also, check How To Use NULL Functions In SQL