// dates & times reference

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.

Categoria
Dates & times
Returns
datetime2 / datetime / datetimeoffset
In this category
84 of 121

Sintaxe

SYSDATETIME()
GETDATE()
SYSDATETIMEOFFSET()

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim 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 Sim SYSTIMESTAMP: server zone; CURRENT_TIMESTAMP: session zone

Exemplos

SQL Server

SELECT SYSDATETIME(), GETDATE(), SYSDATETIMEOFFSET();

MySQL

SELECT NOW(6);  -- microseconds

Oracle

SELECT SYSTIMESTAMP FROM dual;

Bom saber

  • 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.

// fewer lookups

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.