Current Timestamp in SQL
Getting the current timestamp in SQL is needed for auditing (recording when records were created or modified), filtering (finding records within a time range relative to now), scheduling (checking if something has expired), and logging (timestamping events). The exact function name and behavior differs across database systems, but the concept is universal.
A key consideration when working with SQL timestamps is timezone handling. Some databases (like PostgreSQL with timestamptz) store timestamps in UTC and convert to the session timezone for display. Others (like MySQL with DATETIME) store the literal datetime without timezone information. Always be explicit about which timezone you're working in, especially in applications with users in multiple countries.
Current timestamp functions by database
MySQL / MariaDB:
NOW() — DATETIME, local timezone
CURRENT_TIMESTAMP — same as NOW()
UTC_TIMESTAMP() — DATETIME in UTC
UNIX_TIMESTAMP() — INT, seconds since epoch
PostgreSQL:
NOW() — TIMESTAMPTZ, transaction start
CURRENT_TIMESTAMP — same as NOW()
CLOCK_TIMESTAMP() — TIMESTAMPTZ, actual current time
EXTRACT(EPOCH FROM NOW()) — FLOAT, Unix seconds
SQL Server:
GETDATE() — DATETIME, local
GETUTCDATE() — DATETIME, UTC
SYSDATETIMEOFFSET() — DATETIMEOFFSET with timezone
SYSUTCDATETIME() — DATETIME2 in UTC
SQLite:
datetime('now') — TEXT, UTC
strftime('%s','now') — TEXT, Unix seconds
Oracle:
SYSDATE — DATE (with time), local
SYSTIMESTAMP — TIMESTAMP WITH TIME ZONE