SQL UTC_TIMESTAMP function
Returns the current instant in UTC regardless of the server's local zone, the safest value to store for event timestamps.
- Kategorie
- Dates & times
- Returns
datetime / timestamp- In this category
- 21 of 121
Syntax
UTC_TIMESTAMP -- MySQL
GETUTCDATE() -- SQL Server
SYSUTCDATETIME()
now() AT TIME ZONE 'UTC'
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Ja | Supported under this name. |
| PostgreSQL | Different name or syntax | NOW() AT TIME ZONE 'UTC' |
| MySQL | Ja | Supported under this name. |
| SQLite | Different name or syntax | no function needed: datetime('now') is already UTC |
| Oracle | Different name or syntax | SYS_EXTRACT_UTC(SYSTIMESTAMP) |
Beispiele
MySQL
SELECT UTC_TIMESTAMP() AS utc_now;
SQL Server
SELECT SYSUTCDATETIME() AS utc_now;
PostgreSQL
SELECT now() AT TIME ZONE 'UTC' AS utc_now;
SQLite
SELECT strftime('%Y-%m-%d %H:%M:%S','now') AS utc_now;
Gut zu wissen
- SQLite needs no function: CURRENT_TIMESTAMP and datetime('now') are already UTC; append 'localtime' to get the wall clock instead.
- MySQL's UTC_TIMESTAMP and NOW() differ by the session time_zone setting; mixed sessions can write mixed clocks into one table.
- PostgreSQL's NOW() is timestamptz and already an absolute instant; NOW() AT TIME ZONE 'UTC' turns it into a zone-less timestamp holding the UTC wall time.
Questions about UTC_TIMESTAMP / GETUTCDATE
Why store UTC instead of local time?
Local zones shift with DST and politics. Store UTC, convert at the edges, and sorting plus comparisons stay correct forever.
Does SQLite really need no function for UTC?
Right. Its clock is UTC by default, which is why local lookups there use the 'localtime' modifier instead.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
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.