// dates & times reference

SQL SYSDATETIMEOFFSET function

Returns the current date and time as a datetimeoffset carrying the server's UTC offset, with 100-nanosecond precision.

カテゴリ
Dates & times
Returns
datetimeoffset
In this category
20 of 121

構文

SYSDATETIMEOFFSET()
SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00')
TODATETIMEOFFSET(d, '+02:00')

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() is timestamptz, the zone-aware equivalent
MySQL Not built in no zone-aware type; NOW() only
SQLite Not built in no zone-aware type
Oracle Different name or syntax SYSTIMESTAMP

例

SQL Server

SELECT SYSDATETIMEOFFSET() AS now_local;

SQL Server

SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'UTC' AS now_utc;

知っておくと便利なこと

  • The offset comes from the server operating system's time zone; SYSDATETIMEOFFSET() AT TIME ZONE 'Pacific Standard Time' converts to a named zone instead of trusting it.
  • TODATETIMEOFFSET tags a zone-less value with an offset you supply; SWITCHOFFSET keeps the instant and changes the offset. Mixing them up shifts the time.
  • Only SQL Server's datetimeoffset and Oracle's TIMESTAMP WITH TIME ZONE keep the offset. PostgreSQL's timestamptz normalizes to UTC and forgets the input offset; MySQL and SQLite store zone-less values.

Questions about SYSDATETIMEOFFSET

How do I convert SYSDATETIMEOFFSET to UTC?

SYSDATETIMEOFFSET() AT TIME ZONE 'UTC', or SYSUTCDATETIME() directly. SWITCHOFFSET(..., '+00:00') gives the same instant.

What is the difference between TODATETIMEOFFSET and SWITCHOFFSET?

TODATETIMEOFFSET attaches an offset to a zone-less value, changing the instant if the value was local. SWITCHOFFSET keeps the instant and rewrites the offset.

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.