// dates & times reference

SQL current UTC timestamp

The current instant expressed in UTC: the timestamp shape to persist for events, logs and any cross-zone comparison.

カテゴリ
Dates & times
Returns
timestamp
In this category
114 of 121

構文

UTC_TIMESTAMP()
SYSUTCDATETIME()
datetime('now')

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

エンジンサポート

エンジンSupportメモ
SQL Server はい SYSUTCDATETIME() / GETUTCDATE()
PostgreSQL はい now() AT TIME ZONE 'UTC'
MySQL はい UTC_TIMESTAMP()
SQLite はい strftime('%Y-%m-%d %H:%M:%S', 'now')
Oracle はい SYS_EXTRACT_UTC(SYSTIMESTAMP)

例

MySQL

SELECT UTC_TIMESTAMP(), NOW();  -- differ by session offset

SQLite

SELECT strftime('%Y-%m-%dT%H:%M:%SZ', 'now');

PostgreSQL

SELECT now() AT TIME ZONE 'UTC';

知っておくと便利なこと

  • SQLite's 'now' is UTC natively; the localtime modifier is the opt-in. Every other engine's local default needs the UTC spelling above.
  • Oracle's SYSDATE and SYSTIMESTAMP are the *database server's* zone, not the session's; UTC conversion is SYS_EXTRACT_UTC(SYSTIMESTAMP), and neither touches session TIME_ZONE.
  • MySQL's UTC_TIMESTAMP and NOW() differ by the session offset; mixing them in one table is how people get rows dated 2 hours in the future.

Questions about UTC_TIMESTAMP

Is UTC 'now' affected by DST?

Never. UTC has no offsets or transitions, which is precisely why it is the storage zone for event times.

Why is my UTC timestamp still wrong?

Usually the data was built from a local clock with a hardcoded offset. Compute UTC inside the database with the functions above instead of string surgery in app code.

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.