2017-02-15 53 views
1

我仍然相当新的Teradata所以请原谅这一点,但我有两列一个日期和一个有4位VARCHAR作为时间(24小时)Teradata的串联多个字符串列的格式时间戳

下面

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

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

这是我从上面的查询得到的结果的一个例子。 2017年1月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 

查询我得到的时间戳无效

回答

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 | 
+---------------+------------+----------------------------+ 

附:
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  | 
+------------+------------+------------+-----------+-----------+