SQL 尝试在 Teradata 中投射日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23519420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to cast date and time in Teradata
提问by Doug Fir
I'm selecting from a table with a timestamp(6) data type.
我正在从具有时间戳 (6) 数据类型的表中进行选择。
I would like to get the date and time, to the minute, like so:
我想以分钟为单位获取日期和时间,如下所示:
Currently the data look like this: 03/10/2014 21:54:47.690000
目前的数据是这样的: 03/10/2014 21:54:47.690000
I would like to select so that the result looks like this: 03/10/2014 21:54:47
我想选择以便结果如下所示: 03/10/2014 21:54:47
I tried this:
我试过这个:
SELECT CAST(EVENT_GMT_TIMESTAMP AS DATE) || (EVENT_GMT_TIMESTAMP AS TIME) AS theDate, SUM(VwNIMEventFct.DWELL_MINUTES) AS totalDwellMins
FROM RDMAVWSANDBOX.VwNIMEventFct
WHERE EXTRACT(MONTH FROM EVENT_GMT_TIMESTAMP) = 4
GROUP BY theDate
But received a message "Syntax error, expected something between the word EVENT_GMT_TIMESTAMP and the AS keyword"
但是收到一条消息“语法错误,在单词 EVENT_GMT_TIMESTAMP 和 AS 关键字之间需要一些东西”
Also tried concat().
也试过 concat()。
Is there a better way, rather than concatenating, to select EVENT_GMT_TIMESTAMP as datetime but to the minute?
有没有更好的方法,而不是连接,选择 EVENT_GMT_TIMESTAMP 作为日期时间但到分钟?
Also to the hour?
还到小时?
There must be a function that lets you select the output like this?
一定有一个功能可以让您像这样选择输出吗?
The ones I found researching the web do not appear to function on Teradata.
我在研究网络时发现的那些在 Teradata 上似乎不起作用。
I also tried this:
我也试过这个:
SELECT CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMM')
But the output was like this:
但输出是这样的:
04/18/2014 12:20:44.384000
I need it to be like this:
我需要它是这样的:
04/18/2014 12:20:44
回答by Andrew
I've never been able to get timestamp formatting to work quite like I want it, but this will get you the desired format:
我从来没有能让时间戳格式像我想要的那样工作,但这会让你得到想要的格式:
SELECT current_timestamp(6) AS ts,
( ts (FORMAT 'MM/DD/YYYY') (CHAR(10)))as DT,
cast (ts as time(6)) (char(8)) as tm,
dt ||' ' ||tm as FormattedDate
Or, there's the new and improved version, compliments of dnoeth:
或者,有新的和改进的版本,dnoeth 的赞美:
SELECT current_timestamp(6) AS ts,
ts (FORMAT 'MM/DD/YYYYbhh:mi:ss') (CHAR(19))
回答by imran
CAST(Column AS TIMESTAMP(0))
is a better way to do
是更好的方法