zhyiwww
用平实的笔,记录编程路上的点点滴滴………
posts - 536,comments - 394,trackbacks - 0

比如,我有一列数据,字段名是logtime,数据如下:
"2009年07月13日10时45分45秒"
"2009年07月13日10时45分45秒"
"2009年07月13日10时45分45秒"
"2009年07月13日10时45分45秒"
"2009年07月13日10时45分52秒"
"2009年07月13日10时45分52秒"
"2009年07月13日10时45分56秒"
"2009年07月13日10时45分56秒"
"2009年07月13日10时46分09秒"

我想把其转成timestamp类型。我的方法如下:
(1)把上面的字符串替换成时间类型的格式
"2009-07-13 10:17:37"
"2009-07-13 10:17:37"
"2009-07-13 10:17:53"
"2009-07-13 10:18:10"
"2009-07-13 10:18:10"
"2009-07-13 10:18:19"
"2009-07-13 10:18:39"
"2009-07-13 10:18:39"

替换方法是:
feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'年','-') ;
feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'月','-') ;
feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'日',' ') ;

feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'时',':') ;
feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'分',':') ;
feitian_logs=# update ivr_detail_log set logtime = replace(logtime,'秒','') ;

替换完毕,就是上面的时间格式了。

(2)转换数据类型
我用的是比较传统和保守的方法
# alter ivr_detail_log add column start_time timestamp;
# update ivr_detail_log set start_time = to_timestamp(logtime,'YYYY-MM-DD HH:MI:SS');

这就完成了。
当然也可以直接把改变字段类型。

#






|----------------------------------------------------------------------------------------|
                           版权声明  版权所有 @zhyiwww
            引用请注明来源 http://www.blogjava.net/zhyiwww   
|----------------------------------------------------------------------------------------|
posted on 2009-07-17 17:00 zhyiwww 阅读(4244) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: