SQL Server Tutorial
Date functions are used to manipulate datetime values (arithmetic operations) and parsing date components.
SELECT GETDATE()Output:
2017-11-26 01:59:19.040
Adds the number of date parts to the date.
SELECT DATEADD(year, 2, GETDATE())
Adds current date with 2 years.
SELECT DATEADD(year, 2, '2019-11-26 02:04:52.390')Output:
2021-11-26 02:04:52.390
Instead of year, we can also use either yy or yyyy abbreviations.
SELECT DATEADD(yy, 2, '2019-11-26 02:04:52.390')
DATEDIFF Function is used to calculate the date difference as date part between dates.
SELECT DATEDIFF(month, '2019-08-26 02:04:52.390', '2019-11-26 02:04:52.390')Output:
3
returns number of month difference between dates '2019-08-26 02:04:52.390' and '2019-11-26 02:04:52.390'.
DATENAME function is used to return datepart as string in the specified date.
SELECT DATENAME(month, '2019-11-26 02:04:52.390')Output:
November
DATEPART function is used to return datepart as integer in the specified date.
SELECT DATEPART(month, '2019-11-26 02:04:52.390')Output:
11
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page