SQL SYSDATETIMEOFFSET function
Returns the current date and time as a datetimeoffset carrying the server's UTC offset, with 100-nanosecond precision.
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, 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.