SQL SYSDATETIME function
Server clocks come in three precisions on SQL Server and two styles on Oracle; picking the wrong one quietly rounds your timestamps.
- Категория
- Dates & times
- Returns
datetime2 / datetime / datetimeoffset- In this category
- 84 of 121
Синтаксис
SYSDATETIME()
GETDATE()
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 | clock_timestamp() for real time; now() is transaction-start |
| MySQL | Different name or syntax | NOW(6) for microseconds; NOW() truncates to seconds |
| SQLite | Different name or syntax | strftime('%f','now') carries milliseconds |
| Oracle | Да | SYSTIMESTAMP: server zone; CURRENT_TIMESTAMP: session zone |
Примеры
SQL Server
SELECT SYSDATETIME(), GETDATE(), SYSDATETIMEOFFSET();
MySQL
SELECT NOW(6); -- microseconds
Oracle
SELECT SYSTIMESTAMP FROM dual;
Полезно знать
- GETDATE returns datetime (~3.33 ms precision, always rounded); SYSDATETIME returns datetime2 with 100 ns precision. Written into a datetime column, the value is still rounded to .000, .003 or .007 seconds.
- Oracle splits by zone, not precision: SYSTIMESTAMP is the server's zone, CURRENT_TIMESTAMP the session's (the classic why-is-this-two-hours-off bug).
- PostgreSQL's now() is fixed at transaction start so all statements in one transaction agree; clock_timestamp() advances within the transaction.
Questions about SYSDATETIME / GETDATE
Which clock should new SQL Server code use?
SYSDATETIME() for local wall time, SYSDATETIMEOFFSET() when the offset matters. GETDATE survives for compatibility with datetime columns.
Why do all rows in my transaction share the same timestamp?
PostgreSQL's now() and CURRENT_TIMESTAMP are fixed at transaction start; call clock_timestamp() if you need time that advances.
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.