Oracle - 将char转换为日期

时间:2015-06-07 18:35:28

标签: sql oracle date type-conversion

我尝试将旧的char(8)列转换为新的time_conv日期列。

String列具有以下格式:

小时(0-99):分钟(0-59):秒(0:59)

例如:01:32:56

以下代码对我不起作用:

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

修改

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*****'); 

2 个答案:

答案 0 :(得分:1)

如果要将值转换为秒,则只需使用算术:

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

答案 1 :(得分:0)

此代码适用于我:)

  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))
       );

谢谢大家!