将时间(char(8))添加到日期列

时间:2015-06-09 18:08:30

标签: oracle datetime plsql oracle-sqldeveloper date-formatting

我尝试合并日期和时间列。

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

alter table teldat add (date_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*****');

update teldat set date_conv=(to_date(substr(date, 1, 2)+substr(date, 4, 2)+substr(date, 6, 2)+substr(time, 1, 2)+substr(time, 4, 2),'dd.mm.yy hh24:mi'));

如果我执行update语句,则会出现以下错误消息:

Error starting at line : 1 in command -
update teldat set date_conv=(to_date(substr(date, 1, 2)+substr(date, 4, 2)+substr(date, 6, 2)+substr(time, 1, 2)+substr(time, 4, 2),'dd.mm.yy hh24:mi'))
Error at Command Line : 1 Column : 45
Error report -
SQL Error: ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:  *Action:

没有解决方案。谁能帮我?

亲切的问候

3 个答案:

答案 0 :(得分:0)

此更新有效:

update teldat set 
  date_conv = to_date(to_char(tdate, 'yyyymmdd')||uhrzeit,'yyyymmddhh24:mi');

SQLFiddle

我假设您想要第二列(uhrzeit)作为时间部分,但如果您想要下一列 - 使用它(使用substr(...,5))。此外,我将列名date更改为tdate,因为date是Oracle关键字,因此最好避免将其用作列名。

答案 1 :(得分:0)

您可以进行以下更正并尝试:

  1. 将列名称从日期更改为日期(任何名称),因为DATE是oracle关键字

  2. 您插入的值的数量是7,小于改变(添加)1列(7 + 1 = 8)后表中的列数。因此再添加一个值。

  3. 更正更新声明(见下文)

  4.   

    改变会话集NLS_DATE_FORMAT =' DD-MON-YYYY HH24:MI:SS' ;

    CREATE TABLE teldat(
        dates   DATE,
        uhrzeit     CHAR(8),
        time        CHAR(8),
        teilnehmer  NUMBER(3),
        verbart     NUMBER(1),
        aufbauart   CHAR(3),
        ziel        VARCHAR(15));
    
    alter table teldat add (date_conv date);
    
    INSERT INTO TELDAT VALUES (to_date('04.08.2011','dd.mm.yyyy'),'17:33 ', '00:00:40',10,9, 'K10', NULL,
    to_date('13.08.2011','dd.mm.yyyy'));
    
    select* from teldat;
    
    update teldat  set date_conv= 
    TO_DATE(
     substr(dates, 1, 2) || '-'  || 
     substr(dates, 4, 3) || '-'  ||
     substr(dates, 8, 4) || '-'  || 
     substr(time, 1, 2) || ':'   ||
     substr(time, 4, 2) 
     ,'DD-MON-YYYY HH24:MI')
    ; 
    
    commit;
    
      

    输出:

    OUTPUT

答案 2 :(得分:0)

SQLDeveloper菜单:工具,首选项,数据库,更改NLS日期格式

update teldat set datum_conv=to_date(to_char(datum, 'dd.mm.yyyy')||uhrzeit,'dd.mm.yyyy hh24:mi');
select to_char(datum_conv, 'dd.mm.yyyy hh24:mi') from teldat ;

适合我! 谢谢你所有的帮助! :)

亲切的问候