EXTRACT the date and time - (Teradata)

不问归期 提交于 2019-12-14 03:59:50

问题


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?


回答1:


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 :-)




回答2:


I know this is an old topic, but I've struggled with this too. Try:

CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))

The result will be

01/02/2012 12:18:59

The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).



来源:https://stackoverflow.com/questions/23506057/extract-the-date-and-time-teradata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!