Oracle虚拟列,日期和时间戳

时间:2014-10-23 09:57:54

标签: oracle date timestamp virtual-column

我在Oracle中有一个问题,有一个虚拟列(我需要从日期列开始的时间戳);这是我的例子:

CREATE TABLE TBDATETIME(
  DATETIME_1 DATE,
  DATETIME_2 TIMESTAMP(9) GENERATED ALWAYS AS (
    CAST( TO_CHAR(DATETIME_1, 'DD/MM/YYYY HH24:MI:SS')
      || '.' || TO_CHAR(DATETIME_1, 'FF9')  AS TIMESTAMP(9))
   ) VIRTUAL
 );

INSERT INTO TBDATETIME(DATETIME_1)
VALUES(SYSDATE);

COMMIT;

SELECT *
FROM TBDATETIME;

ORA-01821: date format not recognized
01821. 00000 -  "date format not recognized"
*Cause:    
*Action:

获取数据的问题在哪里?我尝试了很多格式面具,但没有任何帮助...

1 个答案:

答案 0 :(得分:4)

DATETIME_1 DATE

TO_CHAR(DATETIME_1, 'FF9')

这就是问题 - DATE没有分数秒

将char转换为时间戳会很危险(结果取决于nls设置)

我想你想要这个:

CREATE TABLE TBDATETIME(DATETIME_1 DATE, DATETIME_2 TIMESTAMP(9) GENERATED ALWAYS AS (CAST(DATETIME_1 AS TIMESTAMP(9))) VIRTUAL);