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 |
SQL
|
Number of Records: 1
|
Examples: Just date, Just Time, Just Day, Month or Year
|
current database system info |
SQL |
Result |
|---|---|---|
date and time
|
SQL
|
2024-05-05 20:08:57.923 |
date
|
SQL
|
2024-05-05 |
time
|
SQL
|
20:08:57.923 |
year
|
SQL
|
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
-
https://www.sqlshack.com/sql-server-getdate-function-and-its-use-cases/ – sqlshack.com. GETDATE function with DATENAME, DATEPART, DATEADD, DAY, MONTH, EOMONTH, and YEAR functions. Convert date time format using CAST and CONVERT.