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
Check these values
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.
Your result
How it was worked out
How to use the converter
- Type the Unix time and choose what it counts: seconds, milliseconds or microseconds.
- Type the UTC offset of the local time you want, such as
+05:30, and the name of the column that holds the number. - Choose Convert. You get the UTC and local date and time, a literal of the value, and the expression for each engine.
- 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
| 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)andUNIX_TIMESTAMP(d)use the time zone of the session, andFROM_UNIXTIMEgives NULL before 1970. PostgreSQLTO_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,
timestampis a row version counter, not a date.
Other tools for the same subject
- To see one date in many formats (ISO 8601, Excel, Julian), use the Date Format Converter.
- To format a date as text or read text as a date in SQL, use the SQL Date Format Converter.
- For named time zones with summer time, use the Time Zone Converter.
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.