// conversion reference

SQL TO_TIMESTAMP function

Parses text into a timestamp using a format mask, keeping hours, minutes, seconds and fractions. On PostgreSQL the one-argument form converts Unix epoch seconds instead.

类别
Conversion
Returns
timestamptz on PostgreSQL, TIMESTAMP on Oracle
In this category
5 of 39

语法

TO_TIMESTAMP(text, format)
TO_TIMESTAMP(epoch_seconds)  -- PostgreSQL

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

引擎支持

引擎Support备注
SQL Server Different name or syntax CONVERT(datetime2, text, style) or CAST
PostgreSQL 是 Supported under this name.
MySQL Different name or syntax STR_TO_DATE(text, '%Y-%m-%d %H:%i:%s')
SQLite Not built in datetime(text) parses ISO 8601 only
Oracle 是 Supported under this name.

示例

PostgreSQL

SELECT TO_TIMESTAMP('2026-08-01 14:30:00', 'YYYY-MM-DD HH24:MI:SS');

Oracle

SELECT TO_TIMESTAMP('2026-08-01 14:30:00', 'YYYY-MM-DD HH24:MI:SS') FROM dual;

SQL Server

SELECT CONVERT(DATETIME2, '2026-08-01 14:30:00', 120);

值得了解

  • PostgreSQL's TO_TIMESTAMP always returns timestamptz. The text is read in the session time zone unless the mask includes an offset (TZH:TZM), so the same call stores different instants in sessions with different TimeZone settings.
  • PostgreSQL's one-argument TO_TIMESTAMP takes a number of seconds since 1970: TO_TIMESTAMP(1785000000) is 2026-07-25 17:20:00+00. Oracle's TO_TIMESTAMP returns a TIMESTAMP without a zone; TO_TIMESTAMP_TZ keeps one.
  • The mask codes mirror TO_CHAR: PostgreSQL/Oracle 'YYYY-MM-DD HH24:MI:SS', MySQL '%Y-%m-%d %H:%i:%s'.
  • SQLite's datetime() accepts ISO 8601 text only; anything else needs REPLACE surgery to ISO first.

Questions about TO_TIMESTAMP

What is the difference between TO_TIMESTAMP and TO_DATE?

On PostgreSQL, TO_DATE returns a date and drops the time; TO_TIMESTAMP keeps the time and returns timestamptz. On Oracle both keep the time of day, because Oracle's DATE has seconds; TO_TIMESTAMP adds fractional seconds.

What is the equivalent of TO_TIMESTAMP in MySQL?

STR_TO_DATE(text, '%Y-%m-%d %H:%i:%s') with MySQL's percent-format codes.

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.