EXTRACT isolates and returns an individual date- or time-part as a numeric value from a date, time, timestamp, or interval expression. The function is ANSI-SQL compliant and widely implemented across major databases. Unlike DATE_TRUNC, which zeroes out lower-order fields, EXTRACT only pulls the requested part, leaving the source value unchanged. The returned data type is integer or numeric depending on the part. For intervals, EXTRACT can return larger units such as epoch (total seconds). When applied to TIMESTAMP WITH TIME ZONE, the value is first converted to the session time zone before extraction. Null input results in null output. Precision beyond supported range triggers an error in some systems.
field
(text) - Date or time part to extract (e.g., YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, EPOCH, DOW, DOY).source
(date/time expression) - A column, literal, or expression of type DATE, TIME, TIMESTAMP, or INTERVAL.DATE_PART, DATE_TRUNC, TO_CHAR, INTERVAL, TIMESTAMP, CAST
SQL-92
EXTRACT returns only the specified component as a number, while DATE_TRUNC resets all lower-order fields to zero and returns a full timestamp.
Many databases allow `SECOND` with a fractional part. For finer resolution, multiply the fractional seconds: `EXTRACT(EPOCH FROM ts) * 1000`.
EXTRACT returns NULL when the source expression is NULL.
Yes. For TIMESTAMP WITH TIME ZONE values, most systems convert to the current session time zone before extracting, which can change the result of fields like HOUR or DOW.