SQL Keywords

SQL TIMESTAMP

What is the SQL TIMESTAMP data type?

Stores both date and time values, optionally with fractional seconds and time-zone awareness.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL TIMESTAMP: PostgreSQL, MySQL, MariaDB, SQL Server (as DATETIME2 / DATETIMEOFFSET), Oracle, SQLite, Snowflake, BigQuery

SQL TIMESTAMP Full Explanation

TIMESTAMP is a core SQL data type that records an absolute point in time to the second or sub-second. Unlike DATE (date only) and TIME (time only), TIMESTAMP keeps both components in a single field, making it ideal for event logging, audit trails, and temporal analytics.Precision - Most dialects let you specify fractional‐seconds precision in parentheses, for example TIMESTAMP(3) for millisecond resolution. The valid range is typically 0-6 digits, where 6 equals microseconds.Time zone - Standard SQL defines TIMESTAMP WITHOUT TIME ZONE and TIMESTAMP WITH TIME ZONE. The latter normalizes values to UTC while remembering the original offset, ensuring global consistency. Some engines (MySQL, SQLite) ignore the WITH TIME ZONE variant, while PostgreSQL treats TIMESTAMP as WITHOUT TIME ZONE unless explicitly stated.Defaults and auto-update - CURRENT_TIMESTAMP (or NOW()) can be assigned as a default to auto-populate the current clock time at insert. In MySQL, you can also add ON UPDATE CURRENT_TIMESTAMP for automatic touch-timestamps.Storage - Internally, vendors store TIMESTAMP as either an 8-byte integer (Unix epoch microseconds) or a composite structure. Range usually spans 4713 BC to 294276 AD (PostgreSQL) or 1970-2038 (older MySQL when using 4-byte representation).Caveats - Be explicit about time zones. Mixing WITH and WITHOUT TIME ZONE leads to silent conversions. Fractional seconds above the engine limit round or raise errors. Always index high-write TIMESTAMP columns used in WHERE clauses to avoid slow scans.

SQL TIMESTAMP Syntax

column_name TIMESTAMP[(fractional_seconds_precision)] [WITHOUT TIME ZONE | WITH TIME ZONE] [DEFAULT CURRENT_TIMESTAMP]

SQL TIMESTAMP Parameters

  • fractional_seconds_precision (integer) - number of digits after the decimal point (0-6)
  • WITH TIME ZONE (flag) - stores offset information and converts to UTC internally
  • DEFAULT CURRENT_TIMESTAMP (expression) - auto-assigns the current date-time at insert

Example Queries Using SQL TIMESTAMP

-- 1. Table with millisecond precision timestamp
CREATE TABLE page_views (
  id            SERIAL PRIMARY KEY,
  user_id       INT NOT NULL,
  viewed_at     TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP
);

-- 2. Time-zone aware timestamp in PostgreSQL
CREATE TABLE messages (
  msg_id INT PRIMARY KEY,
  body   TEXT,
  sent_at TIMESTAMP WITH TIME ZONE
);

-- 3. Insert explicit value
INSERT INTO page_views (user_id, viewed_at)
VALUES (42, '2024-05-01 13:45:10.123');

-- 4. Filter by timestamp range
SELECT *
FROM page_views
WHERE viewed_at BETWEEN '2024-05-01 00:00:00' AND '2024-05-02 00:00:00';

Expected Output Using SQL TIMESTAMP

  • Table definitions succeed and create columns capable of holding full date-time values.
  • Inserts populate viewed_at with the provided literal or the current clock time when omitted.
  • SELECT returns rows whose viewed_at falls inside the specified window.

Use Cases with SQL TIMESTAMP

  • Track created_at and updated_at columns on transactional tables
  • Log user activity or page views with millisecond precision
  • Store scheduled job execution times
  • Perform cohort analyses or retention curves based on event timestamps
  • Synchronize data across systems that need a canonical time reference

Common Mistakes with SQL TIMESTAMP

  • Assuming TIMESTAMP always has a time zone. Many engines default to WITHOUT TIME ZONE.
  • Forgetting to specify precision, leading to truncation of required sub-seconds.
  • Storing local time without offset, then comparing across regions.
  • Relying on MySQL's deprecated implicit '0000-00-00 00:00:00' zero timestamp.
  • Comparing TIMESTAMP to DATE literals, causing implicit casts and index skips.

Related Topics

DATE, TIME, DATETIME, CURRENT_TIMESTAMP, NOW(), INTERVAL, TIME ZONE

First Introduced In

SQL-92

Frequently Asked Questions

What is the difference between TIMESTAMP and DATETIME?

TIMESTAMP follows the SQL standard and can be time-zone aware, whereas DATETIME is proprietary and usually lacks automatic zone conversion. Check your DBMS docs before choosing.

How do I auto-populate created_at and updated_at columns?

Define created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP and updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP (MySQL) or use a trigger in PostgreSQL/Oracle.

Why does my TIMESTAMP value shift after daylight saving changes?

You are using TIMESTAMP WITH TIME ZONE. The database converts the stored UTC time to your session's zone, so apparent values move when the offset changes.

Can I index TIMESTAMP columns?

Yes. Adding an index on frequently filtered TIMESTAMP columns (e.g., created_at) speeds up range queries and ORDER BY operations.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!