Toolvito

Works on this device

Unix Timestamp to SQL Date

Turn a Unix timestamp into an SQL date and time, and see the SQL that converts a whole column in PostgreSQL, MySQL, SQL Server, Oracle, SQLite and Snowflake. Choose seconds, milliseconds or microseconds and a UTC offset. It also works the other way, from a date and time to Unix time.

Enter your values

Type a Unix time in seconds, milliseconds or microseconds. You get the UTC and local date and time, the SQL literal and the expression that converts the column in each engine.

Your values

Seconds since 1 January 1970, 00:00:00 UTC. A minus sign gives a date before 1970.

Such as +05:30 or -08:00. A fixed offset: named time zones and summer time are not used.

The column (or a variable such as @t) that the SQL reads. It is written into the expressions as typed.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Convert.

How to use the converter

  1. Type the Unix time and choose what it counts: seconds, milliseconds or microseconds.
  2. Type the UTC offset of the local time you want, such as +05:30, and the name of the column that holds the number.
  3. Choose Convert. You get the UTC and local date and time, a literal of the value, and the expression for each engine.
  4. For the other way, choose An SQL date and time to Unix time.

The rule

  • Unix time is the count of seconds since 1970-01-01 00:00:00 UTC. Leap seconds are not counted. A negative number is a moment before 1970.
  • Milliseconds are seconds × 1,000 and microseconds are seconds × 1,000,000. Today a Unix time has 10 digits in seconds and 13 in milliseconds.
  • The expressions add the number to 1970-01-01 00:00:00 in UTC, then add the offset in minutes. So they do not depend on the time zone setting of the session.

Expressions for a column of seconds

Column c of Unix seconds to a UTC date and time
Engine Expression
PostgreSQL 16 and 17 TIMESTAMP '1970-01-01 00:00:00' + c * INTERVAL '1 second'
MySQL 8.0 and 8.4 TIMESTAMP '1970-01-01 00:00:00' + INTERVAL c SECOND
SQL Server 2019 and 2022 DATEADD(second, c % 86400, DATEADD(day, c / 86400, CAST('1970-01-01' AS datetime2)))
Oracle Database 19c and 23ai TIMESTAMP '1970-01-01 00:00:00' + NUMTODSINTERVAL(c, 'SECOND')
SQLite 3.39 and later datetime(c, 'unixepoch')
Snowflake TO_TIMESTAMP_NTZ(c)

Worked examples

Unix time 1700000000 (seconds) is 2023-11-14 22:13:20 UTC, a Tuesday. At UTC+05:30 it is 2023-11-15 03:43:20. In SQL Server the literal is CAST('2023-11-14T22:13:20' AS datetime2).

The same moment in milliseconds is 1700000000000. The number -1500 in milliseconds is 1969-12-31 23:59:58.500 UTC.

The other way: 2023-11-15 03:43:20 at +05:30 is Unix time 1700000000.

Things that often go wrong

  • A 13-digit number read as seconds gives a year far in the future: it is milliseconds.
  • MySQL FROM_UNIXTIME(c) and UNIX_TIMESTAMP(d) use the time zone of the session, and FROM_UNIXTIME gives NULL before 1970. PostgreSQL TO_TIMESTAMP(c) gives a timestamp with time zone, shown in the time zone of the session.
  • SQL Server DATEADD(second, c, '1970-01-01') stops working after 2038-01-19 03:14:07 UTC, because DATEADD takes an int. The expressions here split the number into days first.
  • In SQL Server, timestamp is a row version counter, not a date.

Other tools for the same subject

Limits you should know

  • A fixed UTC offset only. Named time zones and summer time need a time zone database, which this tool does not use.
  • Dates from the year 1 to 9999. SQLite keeps milliseconds at most.
  • No SQL is run here. Test each expression in your own database version.

Related tools