// dates & times reference

SQL GETDATE function

Returns the current SQL Server date and time. GETDATE() rounds to about 3 milliseconds; SYSDATETIME() carries full 100-nanosecond precision on datetime2.

カテゴリ
Dates & times
Returns
datetime / datetime2 / datetimeoffset
In this category
13 of 121

構文

GETDATE()
SYSDATETIME()
GETUTCDATE() / SYSUTCDATETIME()
SYSDATETIMEOFFSET()

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()
MySQL Different name or syntax NOW()
SQLite Different name or syntax datetime('now')
Oracle Different name or syntax SYSDATE

例

SQL Server

SELECT GETDATE() AS loaded_at;

SQL Server

SELECT SYSDATETIME() AS precise_ts;

SQL Server

SELECT CAST(GETDATE() AS date) AS today;

知っておくと便利なこと

  • GETDATE() returns datetime, which rounds to increments of .000, .003 and .007; a value can differ from SYSDATETIME() by up to 2 ms.
  • Each GETDATE() reference is evaluated once per query, not once per row, so an UPDATE of a million rows stamps them all with one value. SYSDATETIME() behaves the same.
  • For UTC, use GETUTCDATE() or SYSUTCDATETIME(). Subtracting a fixed offset from GETDATE() is wrong for half the year wherever daylight saving applies.

Questions about GETDATE / SYSDATETIME

Why does GETDATE() give .003 and .007 values?

datetime stores ticks of 1/300 second, so values round to .000, .003 and .007. SYSDATETIME() on datetime2 keeps real 100 ns precision.

How do I get the UTC time on SQL Server?

GETUTCDATE() (datetime) or SYSUTCDATETIME() (datetime2). A hard-coded offset subtracted from GETDATE() breaks every time daylight saving starts or ends.

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.