Unix Timestamp in SQL
Functions vary by database. Examples below cover PostgreSQL, MySQL, and SQLite - three of the most common.
Common Operations
Current Unix timestamp (PostgreSQL)
SELECT EXTRACT(EPOCH FROM NOW())::BIGINT; -- 1706745600
Current Unix timestamp (MySQL)
SELECT UNIX_TIMESTAMP(); -- 1706745600
Current Unix timestamp (SQLite)
SELECT strftime('%s', 'now');
-- '1706745600'
Convert Unix timestamp to date (PostgreSQL)
SELECT TO_TIMESTAMP(1706745600); -- 2024-01-31 22:40:00+00
Convert Unix timestamp to date (MySQL)
SELECT FROM_UNIXTIME(1706745600); -- 2024-01-31 22:40:00
Convert Unix timestamp to date (SQLite)
SELECT datetime(1706745600, 'unixepoch'); -- '2024-01-31 22:40:00'