T-SQL GETDATE() Function
The GETDATE()
function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.
Examples
Return the current database system date and time | SELECT GETDATE(); | Number of Records: 1 |
Examples: Just date, Just Time, Just Day, Month or Year
current database system info | SQL | Result |
---|---|---|
date and time | SELECT GETDATE(); | 2024-05-05 20:08:57.923 |
date | SELECT CONVERT(DATE, GETDATE()); | 2024-05-05 |
time | 20:08:57.923 | |
year | 2024 |
Examples: Parts of a Date (Day, Month, Year)
In most cases DATEADD combines extracting parts of a date with addition or subtraction.
Last 7 days:
DATEADD(DAY, - 7, GETDATE())
One calendar month ago:
DATEADD(MONTH, - 1, GETDATE())
NOTES
Works in: | SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse |
---|
Related Date Functions
 | Source of date & time | Format | SQL Standard |
---|---|---|---|
| current database system date and time | 'YYYY-MM-DD hh:mm:ss.mmm' | Microsoft |
| current date and time | 'YYYY-MM-DD hh:mm:ss.mmm' | ANSI |
DATEPART (YEAR, GetDate()) | see GETDATE() | ‘YY’ integer | Microsoft |
DATEADD(…) | argument in () |  |  |
Â
Related Pages
SQL Date Manipulations: CONVERT, DATEADD, EOMonth & More – details about CONVERT() function.
Â
https://www.w3schools.com/sql/sql_ref_sqlserver.asp w3schools.com/sql – list of all T-SQL/SQL Server functions
– sqlshack.com. GETDATE function with DATENAME, DATEPART, DATEADD, DAY, MONTH, EOMONTH, and YEAR functions. Convert date time format using CAST and CONVERT.
Â