简体   繁体   English

Oracle-将char转换为日期

[英]Oracle - convert char to date

I try to convert a old time char(8) column to a new time_conv date column. 我尝试将旧的char(8)列转换为新的time_conv日期列。

The String column have the following format: 字符串列具有以下格式:

Hour (0-99):Minute (0-59):Second (0:59) 小时(0-99):分钟(0-59):第二(0:59)

For Example: 01:32:56 例如:01:32:56

The following code does not work for me: 以下代码对我不起作用:

update teldat set time_conv=to_date(time,'hh24:mi:ss');

Edit: 编辑:

CREATE TABLE teldat(
    datum       DATE,
    uhrzeit     CHAR(8),
    time        CHAR(8),
    teilnehmer  NUMBER(3),
    verbart     NUMBER(1),
    aufbauart   CHAR(3),
    ziel        VARCHAR(15));

alter table teldat add (time_conv date);

INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'17:33 ', '00:00:40',10,9, 'K10', NULL); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'18:50 ', '00:01:41',13,9, 'K10', NULL); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'19:10 ', '00:02:17',21,1, 'G1 ', '01019012896****'); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'19:31 ', '00:11:01',10,9, 'K10', NULL); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'19:52 ', '00:09:47',20,1, 'G11', '077202****'); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'19:49 ', '10:07:02',21,1, 'G1 ', '01019012896****'); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'19:58 ', '00:02:41',21,1, 'G1 ', '01019012896****'); 
INSERT INTO TELDAT VALUES (to_date('04.08.2011'),'20:01 ', '00:02:31',21,1, 'G1 ', '01019012896****'); 
INSERT INTO TELDAT VALUES (to_date('05.08.2011'),'09:03 ', '00:03:02',11,9, 'K10', NULL); 
INSERT INTO TELDAT VALUES (to_date('05.08.2011'),'09:13 ', '00:03:31',10,1, 'G10', '071174****'); 
INSERT INTO TELDAT VALUES (to_date('05.08.2011'),'09:39 ', '00:06:45',13,1, 'G10', '0711707*****'); 

If you want to convert the value to seconds, then just use arithmetic: 如果要将值转换为秒,则只需使用算术:

select (to_number(substr(time, 1, 2)) * 60 * 60 +
        to_number(substr(time, 4, 2)) * 60 +
        to_number(substr(time, 7, 2))
       ) as seconds

This code works for me :) 这段代码对我有用:)

  update teldat set time_conv=(to_number(substr(time, 1, 2)) * 60 * 60 +
        to_number(substr(time, 4, 2)) * 60 +
        to_number(substr(time, 7, 2))
       );

Thank you all guys! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM