Intro
There are many Date/Time functions in SQL SERVER including:
- GETDATE()
- DATEADD()
- DATEDIFF()
- DATEPART()
- ISDATE()
- ...
GETDATE()
Use GETDATE() to return the current date and time
Syntax
GETDATE()
SELECT GETDATE()
| 2019-07-07 23:12:33.223 |
|---|
DATEADD()
Use DATEADD() to add a specific time to a date and return it
Syntax
DATEADD(interval, number, date)
SELECT DATEADD(year, 1, GETDATE()) AS DateAdd;
| DateAdd |
|---|
| 2020-07-07 23:15:02.453 |
DATEDIFF()
Use DATEDIFF() to return the difference between two dates
Syntax
DATEDIFF(interval, date1, date2)
SELECT DATEDIFF(year, '1979/10/08', GETDATE()) AS DateDiff;
| DateDiff |
|---|
| 40 |