Teradata将多个字符串列格式化为时间戳

时间:2017-02-15 20:18:30

标签: sql database teradata

我对Teradata还是比较新的所以请原谅这个,但是我有两个列,一个是日期,一个是4位varchar作为时间(24小时)

下面是我用来连接字段以使其可读但我想将结果作为有效时间戳出来以便我可以执行计算。

施放(SCHEDULE_DATE作为日期格式'yyyy-mm-dd')|| ''|| substr(START_TIME,0,3)|| ':'|| substr(START_TIME,2,2)

这是我从上面的查询得到的结果的一个例子。 2017-01-25 13:30

当我像这样运行查询时

cast(cast(SCHEDULE_DATE as date format 'yyyy-mm-dd') || ' ' || substr(START_TIME,0,3) || ':' || substr(START_TIME,2,2) as Timestamp ) as TESTVALUE

我的TimeStamp无效

1 个答案:

答案 0 :(得分:1)

select  '2017-02-15'    as schedule_date
       ,'2233'          as start_time
       ,to_timestamp (schedule_date || start_time,'yyyy-mm-ddhh24mi')    as ts
;
+---------------+------------+----------------------------+
| schedule_date | start_time | ts                         |
+---------------+------------+----------------------------+
| 2017-02-15    | 2233       | 2017-02-15 22:33:00.000000 |
+---------------+------------+----------------------------+

P.S。
substr参数是错误的 Teradata使用1作为起点。

select  '1234'                  as start_time
       ,substr(start_time,0,3)  as original_1
       ,substr(start_time,2,2)  as original_2
       ,substr(start_time,1,2)  as correct_1
       ,substr(start_time,3,2)  as correct_2
;       
+------------+------------+------------+-----------+-----------+
| start_time | original_1 | original_2 | correct_1 | correct_2 |
+------------+------------+------------+-----------+-----------+
| 1234       | 12         | 23         | 12        | 34        |
+------------+------------+------------+-----------+-----------+