How to Convert a Unix Timestamp to a Date/Time in SQL

Here are examples of converting a Unix timestamp to a date/time value in some of the major RDBMSs.

The Unix timestamp (also known as Unix Epoch time, Unix time, or POSIX time) is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC).

SQL Server

In SQL Server we can use the DATEADD() function:

SELECT DATEADD(s, 1860935119, '1970-01-01');

Result:

2028-12-20 14:25:19.000

The s is an abbreviation for second, as is ss (any three can be used).

An overflow error could occur if the Unix timestamp is a larger number with a higher precision. See How to Convert a Unix Timestamp to a Date/Time Value in SQL Server for a possible way of dealing with this.

Oracle Database

Here’s how we can do it in Oracle Database:

SELECT 
    TO_DATE( '1970-01-01', 'YYYY-MM-DD' ) + NUMTODSINTERVAL( 1650321073, 'SECOND' )
FROM DUAL;

Result:

18-APR-22

Here, we use Oracle’s TO_DATE() function to construct a date of 1970-01-01. We then add our Unix timestamp to that date to get our result. In this case, we use NUMTODSINTERVAL() to convert the Unix timestamp into an interval value. The result is a DATE value.

MySQL

MySQL has the FROM_UNIXTIME() function, which enables us to return a date representation of a Unix timestamp. Therefore, it’s as easy as calling the function with the Unix timestamp:

SELECT FROM_UNIXTIME(1947172351);

Result:

2031-09-14 17:12:31

It’s possible to pass a second argument to specify the format of the output. See FROM_UNIXTIME() Examples – MySQL for an example (or check out the MariaDB example below).

MariaDB

MariaDB also has a FROM_UNIXTIME() function that works in pretty much the same way as its MySQL counterpart:

SELECT FROM_UNIXTIME(1947172351, '%W, %D %M %Y');

Result:

Sunday, 14th September 2031

In this example I passed a second argument that specifies the format of the output date.

The result of FROM_UNIXTIME() is expressed in the current time zone. This can be changed though. See How FROM_UNIXTIME() Works in MariaDB for an example of how changing the time zone can affect the resulting date/time value.

PostgreSQL

PostgreSQL has a TO_TIMESTAMP() function to do the job:

SELECT TO_TIMESTAMP(1912995045);

Result:

2030-08-15 03:30:45+00

SQLite

In SQLite we can use the DATETIME() function with the unixepoch modifier:

SELECT DATETIME(1793956207, 'unixepoch');

Result:

2026-11-06 09:10:07

From SQLite 3.38.0, we can use the auto modifier in place of the unixepoch modifier if we prefer. See Get the Date/Time from a Unix Timestamp in SQLite for more examples.