Convert a Unix Timestamp in SQL
Pick the server, type the number, and get both statements — number to datetime and datetime back to number — with the timezone behaviour that dialect actually applies. Five servers, five different grammars, and only two of them have a function called "epoch".
Dialect-specific conversion
Enter a value to see results below. Enter plain integer Unix seconds; units are never inferred from digit count. Supported years: 0001–9999, or the portable 1970–2038 range for MySQL / MariaDB. Statements are dialect-specific.
The datetime literal substituted above is the UTC rendering of your number. What a server does with a bare string is its own decision: MySQL's UNIX_TIMESTAMP() reads it in the session time_zone, so both snippets explicitly set that connection to +00:00 before converting. SQL Server's datetime2 and Oracle's DATE have no zone field at all and are UTC only because you agreed they are. SQLite treats naive strings as UTC, and the Postgres form above pins the offset with +00. That single difference is where most "my timestamp is eight hours off" reports end.
Nothing here ports
| Dialect | Number → datetime | Datetime → number |
|---|---|---|
| MySQL / MariaDB | FROM_UNIXTIME(n) | UNIX_TIMESTAMP(str) |
| PostgreSQL | to_timestamp(n) | extract(epoch FROM ts) |
| SQLite | datetime(n, 'unixepoch') | strftime('%s', str) |
| SQL Server | DATEADD(second, n, '1970-01-01') | DATEDIFF_BIG(second, '1970-01-01', d) |
| Oracle | TIMESTAMP '1970-01-01' + NUMTODSINTERVAL(n, 'SECOND') | (d - DATE '1970-01-01') * 86400 |
- SQL Server and Oracle use date arithmetic in these examples. Explicitly typed epoch literals avoid ambiguous string parsing. A zone-less datetime is UTC only by convention; normalize offset-bearing data to UTC before taking the difference.
- MySQL's
FROM_UNIXTIMEis session-bound. It renders the number in the connection'stime_zone, so1767225600prints as two different wall clocks on two different servers.UNIX_TIMESTAMP(str)has the same problem in reverse. - SQLite formats Unix epochs in UTC by default. The
localtimemodifier converts to the host's local clock. SQLite has no dedicated date storage class: common representations are TEXT dates, INTEGER Unix seconds and REAL Julian day numbers. - PostgreSQL keeps its own epoch. Internally a
timestampis int64 microseconds from 2000-01-01, not 1970;to_timestampandextract(epoch …)translate at the boundary, so the 1970 you type is not the 1970 on disk. - MySQL
TIMESTAMPcolumns are a 4-byte signed Unix counter: valid range 1970-01-01 00:00:01 to 2038-01-19 03:14:07 UTC, stored as UTC regardless of session zone.DATETIMEis zone-less but still bounded: its supported date range is 1000-01-01 through 9999-12-31. In permissive SQL modes, invalid input may become a zero date such as0000-00-00 00:00:00, not the Unix epoch; strict modes can reject it. Check the server version and SQL mode.
Where "my timestamp is wrong by a whole zone" comes from
A whole-hour discrepancy often means an unstated timezone convention, though fractional offsets also exist. Check whether your dialect applies a session zone, preserves an explicit offset, or simply handles zone-less fields:
- MySQL:
SET time_zone = '+00:00'for the session, or stop relying on it and store UTC — format at the application edge.UNIX_TIMESTAMP('2026-01-01 00:00:00')on a UTC+8 connection returns a value 8 hours off the one you want. - SQL Server:
datetime/datetime2carry no offset, so the base literal is UTC only by convention — compare againstsysutcdatetime(), notgetdate(), and usedatetimeoffsetplusSWITCHOFFSETwhen the zone is real data. - Oracle: a bare
CAST(str AS DATE)is parsed with the session'sNLS_DATE_FORMAT, the widget instead usesTO_DATEwith an explicit format mask. Omitting that mask makes parsing depend on the session and can cause errors or misinterpretation. - Fractional seconds need a different trick per server:
FROM_UNIXTIME(1767225600.123)andto_timestamp(1767225600.123)accept a decimal argument, SQLite wants the'subsec'modifier, and SQL Server needs a second-levelDATEADDchained withDATEADD(ms, …), with each integer offset kept in range (older SQL Server versions requireint).
On SQL Server versions limited to an int DATEADD argument, a single seconds offset stops at 2,147,483,647: split it into days and remaining seconds for later dates. Bigint DATEADD arguments require SQL Server 2025 (17.x) or a supported Azure version; merely casting to bigint does not remove the limit on older versions. The result must still fit the destination date type. Use DATEDIFF_BIG (SQL Server 2016+) for the reverse seconds difference. If you are choosing a storage format today, seconds-vs-milliseconds is a separate decision with its own rules — see milliseconds to seconds and the current Unix time page for the value to paste in.
ANSWERS
01 How do I convert a Unix timestamp to a date in MySQL?
SELECT FROM_UNIXTIME(1767225600); — and note it renders in the session time_zone, so the reproducible answer is SET time_zone = '+00:00'; first, or use CONVERT_TZ(FROM_UNIXTIME(n), @@session.time_zone, '+00:00') inline.
02 Postgres has no FROM_UNIXTIME — what replaces it?
SELECT to_timestamp(1767225600); returns a timestamptz (an absolute instant, displayed through the client's TimeZone). The reverse is SELECT extract(epoch FROM ts);, which returns seconds as numeric on current PostgreSQL versions. A direct ::bigint cast rounds; use floor(extract(epoch FROM ts))::bigint for the containing whole second, including pre-epoch values.
03 Why does SQLite give me a UTC string but my app shows another hour?
Because datetime(n, 'unixepoch') is always UTC and your application renders local time afterwards. Add the 'localtime' modifier only if you genuinely want the server's zone baked into the text you store — it makes the value non-portable.
04 Is there a SQL-standard way to write the epoch?
No — that is the whole point of the dialect table. SQL:2003 defines interval arithmetic, so the portable-ish shape is TIMESTAMP '1970-01-01 00:00:00' + INTERVAL … SECOND, but MySQL, SQL Server and Oracle each spell the addition differently. Generate the statement per dialect instead of hoping one runs everywhere.
05 Should a column store Unix seconds or a datetime type?
Epoch seconds in a BIGINT avoid the signed 32-bit 2038 limit; a plain INT can still overflow. A datetime column is readable in queries but zone-less unless its type provides instant or offset semantics, such as timestamptz or datetimeoffset. Whatever you pick, put the unit in the name (created_at_s, expires_at_ms): a bare 1712345678 in a schema is seconds only by convention.