SQL GETDATE function
Returns the current SQL Server date and time. GETDATE() rounds to about 3 milliseconds; SYSDATETIME() carries full 100-nanosecond precision on datetime2.
- Returns
datetime / datetime2 / datetimeoffset- In this category
- 13 of 121
語法
GETDATE()
SYSDATETIME()
GETUTCDATE() / SYSUTCDATETIME()
SYSDATETIMEOFFSET()
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | Different name or syntax | NOW() |
| MySQL | Different name or syntax | NOW() |
| SQLite | Different name or syntax | datetime('now') |
| Oracle | Different name or syntax | SYSDATE |
範例
SQL Server
SELECT GETDATE() AS loaded_at;
SQL Server
SELECT SYSDATETIME() AS precise_ts;
SQL Server
SELECT CAST(GETDATE() AS date) AS today;
小知識
- GETDATE() returns datetime, which rounds to increments of .000, .003 and .007; a value can differ from SYSDATETIME() by up to 2 ms.
- Each GETDATE() reference is evaluated once per query, not once per row, so an UPDATE of a million rows stamps them all with one value. SYSDATETIME() behaves the same.
- For UTC, use GETUTCDATE() or SYSUTCDATETIME(). Subtracting a fixed offset from GETDATE() is wrong for half the year wherever daylight saving applies.
Questions about GETDATE / SYSDATETIME
Why does GETDATE() give .003 and .007 values?
datetime stores ticks of 1/300 second, so values round to .000, .003 and .007. SYSDATETIME() on datetime2 keeps real 100 ns precision.
How do I get the UTC time on SQL Server?
GETUTCDATE() (datetime) or SYSUTCDATETIME() (datetime2). A hard-coded offset subtracted from GETDATE() breaks every time daylight saving starts or ends.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Current timestamp and its precision in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Date difference filters in WHERE Dialect comparison
- Interval literals for date arithmetic in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the last day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Reserve the next job id Snippet
- Truncate a date to day, month or year in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.