Extracting the Time from a Timestamp in Teradata

How to extract the time of day from a Teradata timestamp with CAST, EXTRACT, FORMAT or TO_CHAR, what each changes, and when a plain string is safer to share.

Pulling the clock time out of a Teradata timestamp sounds like one operation, but it can mean several different things: changing the data type to TIME, reading individual fields such as the hour, or producing a text string that only looks like a time. Each option keeps or loses different information, and each behaves differently once the result leaves the database. This review explains what each approach changes and when a plain text value is the better choice.

Which approach suits your intended use?

A bounded recommendation: use CAST to TIME when the result stays inside SQL and will be compared, sorted or used in time arithmetic. Use EXTRACT when you need a single number, such as the hour for grouping. Convert to a character string when the value is headed for a report, an export file or a person copying it into another tool. The decisive tradeoff is between typed values, which preserve comparison and arithmetic behavior, and text values, which display predictably across clients but lose those capabilities.

Goal | Typical approach | Result type | Main tradeoff
Compare or filter by time of day | CAST(ts AS TIME) | TIME | Display depends on the client tool
Group or count by hour or minute | EXTRACT(HOUR FROM ts) | Numeric | Only one field is kept
Show a fixed HH:MI:SS layout in a report | CAST to a FORMAT, then to CHAR | Character | No longer sortable or computable as a time
Oracle-style formatting | TO_CHAR(ts, 'HH24:MI:SS') | Character | Availability depends on your Teradata release

What each transformation actually changes

The word "extract" is ambiguous here. In Teradata it can refer to the EXTRACT function, which returns one numeric field, or more loosely to taking the time portion of a timestamp. The other important distinction is between stored type changes and presentation changes. CAST and EXTRACT produce new values of a different type. A FORMAT phrase on its own only describes how a value should be displayed; it does not change the underlying data unless the result is also cast to a character type.

-- Change the type: keep only the time of day
SELECT CAST(order_ts AS TIME(0)) AS order_time
FROM sales.orders;

-- Read individual fields as numbers
SELECT EXTRACT(HOUR   FROM order_ts) AS hr,
       EXTRACT(MINUTE FROM order_ts) AS mi,
       EXTRACT(SECOND FROM order_ts) AS sec
FROM sales.orders;

-- Produce text with a fixed layout
SELECT CAST(CAST(order_ts AS FORMAT 'HH:MI:SS') AS CHAR(8)) AS order_time_txt
FROM sales.orders;

-- Oracle-style formatting, where supported
SELECT TO_CHAR(order_ts, 'HH24:MI:SS') AS order_time_txt
FROM sales.orders;

The table and column names above are illustrative. The examples show syntax patterns, not verified output from a specific system; no test results were supplied for this review, so confirm the behavior on your own Teradata release.

  • CAST(ts AS TIME): drops the date and keeps the time of day as a TIME value. The precision you specify, such as TIME(0) or TIME(6), controls how many fractional-second digits survive; check your release documentation for whether extra digits are truncated or rejected.
  • EXTRACT(field FROM ts): returns one component. HOUR and MINUTE are integers, while SECOND includes the fractional part as a decimal, so it may not be a whole number.
  • FORMAT then CHAR: produces a string in the layout you choose. After this step the value is text, so sorting and comparison follow character rules rather than time rules.
  • TO_CHAR: also returns text, with format elements such as HH24, MI and SS. It is not present in every Teradata release.

Time zones and precision

If the source column is TIMESTAMP WITH TIME ZONE, the time you extract can depend on how Teradata handles the zone offset and on the session time zone setting. Casting to a type without a zone, or extracting the hour, may give a different clock time than the one originally recorded. Teradata also supports EXTRACT(TIMEZONE_HOUR ...) and EXTRACT(TIMEZONE_MINUTE ...) for reading the offset itself. Because the exact conversion rules vary by data type and configuration, verify them against your release documentation before relying on the result for cross-region reporting. This is a gap the supplied evidence does not resolve.

Will the result stay readable and usable after copying or sharing?

A TIME value is shown by whatever client displays it, and different clients may render it differently. A FORMAT phrase is commonly honored by BTEQ in field mode but may be ignored by ODBC- or JDBC-based tools, which apply their own formatting. That is why reports often cast the formatted value to CHAR: the string then travels unchanged into spreadsheets, CSV exports and chat messages. No scoped tests of specific destinations were provided, so do not assume that a layout that looks right in one tool will look the same in another.

The usability cost of text runs the other way. Once the time is a string, a spreadsheet or BI tool may treat it as text rather than a time, so calculations and time-based sorting may need a conversion step. Values without leading zeros, such as 9:05 instead of 09:05, also sort incorrectly as text. Accessibility is not specifically affected by these conversions: the output is ordinary digits and colons that screen readers and search handle as normal text, but the time zone context is lost unless you include it explicitly.

  • Keep a typed TIME or numeric value when the next step is SQL filtering, joining, grouping or interval arithmetic.
  • Use a zero-padded character string when people will copy the value or another system will import it as text.
  • Include the zone or state it in a column label when readers in different regions will see the result.
  • For date-and-time filtering on large tables, consider filtering on the original timestamp range rather than wrapping the column in a CAST, since a function on the column can limit how the optimizer uses indexes or partitions.

Bottom line

For work that stays in the database, CAST to TIME or EXTRACT preserves meaning and keeps the value computable. For anything that will be shared, displayed or pasted elsewhere, a plain character string with a fixed, zero-padded layout is the more predictable choice. Before committing to either, check your release's documentation on time zone handling, fractional-second precision and TO_CHAR support, since none of these were confirmed by the supplied evidence.