Skip to content
Advertisement

EXTRACT the date and time – (Teradata)

I am trying to extract the date and time from a field in Teradata.

The field in question is:

VwNIMEventFct.EVENT_GMT_TIMESTAMP

Here is what the data look like:

01/02/2012 12:18:59.306000

I’d like the date and time only.

I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.

DATE_FORMAT() does not appear to work since I’m on Teradata.

How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?

Advertisement

Answer

If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it’s simple Standard SQL:

CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)

If it’s a CHAR you need to apply a FORMAT, too:

CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)

Edit:

For simply changing the display format you need to add a FORMAT and a CAST to a string:

CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))

If you don’t care about display, just want to truncate the seconds:

EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)

Working with timestamps is a bit tricky 🙂

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement