SQL 如何在 DB2 中将 VARCHAR 转换为 TIMESTAMP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14474049/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:15:21  来源:igfitidea点击:

How to convert VARCHAR to TIMESTAMP in DB2

sqldatabasedb2

提问by Lilantha Lakmal

I have a varchar value in BD2 Table like below format

我在 BD2 表中有一个 varchar 值,如下格式

121226145503+0530

I want to convert this varchar value to time stamp format like below

我想将此 varchar 值转换为时间戳格式,如下所示

2012/12/26 14:55:03

采纳答案by Vinayak Pahalwan

In DB2 9.7, you can also use the TO_DATE function:

在 DB2 9.7 中,您还可以使用 TO_DATE 函数:

date(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))

Also, you can use the TRANSLATEfunction

此外,您可以使用该TRANSLATE功能

select 
   date(translate('DD/MM/YYYY',column-with-the-date,'xyz...'))
from
   table

回答by CRPence

with d (cwd) as                                              
 (values( cast( '121226145503+0530' as varchar(20))))        
select cwd as vc_input                                       
 ,  to_date(cwd, 'YYMMDDHH24MISS'      )        as ts_from_vc
 ,  varchar_format  ( to_date(cwd, 'YYMMDDHH24MISS')         
                    , 'YYYY/MM/DD HH24:MI:SS' ) as vc_from_ts
from d                                                       
; -- output from above query follows, as likeness of a report:
....+....1....+....2....+....3....+....4....+....5....+....6....+....
VC_INPUT              TS_FROM_VC                  VC_FROM_TS         
121226145503+0530     2012-12-26-14.55.03.000000  2012/12/26 14:55:03
********  End of data  ********