如何只显示时间

时间:2017-10-13 07:41:14

标签: oracle time plsql oracle11g triggers

enter image description here任何人都可以帮助我如何将Time_done的输出更改为仅限时间(例如:上午5:00)?如果我在TO_CHAR(SYSDATE, 'HH24:MI:SS')上添加SYSDATE,则会收到错误消息

  

ORA-01843:不是有效月份。

以下是EVENTLOGS

的代码和截屏
   CREATE TABLE EVENTLOGS(
        Eventlog_id   Number(3,0)   NOT NULL,
        User_name     Varchar2(20),
        Date_done     Date,
        Time_done     Timestamp,
        Action_done   Varchar2(50),
    CONSTRAINT PK_EVENTLOGS PRIMARY KEY (Eventlog_id));

/*  TRIGGER CREATION  */
CREATE OR REPLACE TRIGGER SUPERHEROES_AUDIT
AFTER INSERT OR DELETE OR UPDATE ON SUPERHEROES
FOR EACH ROW
ENABLE
DECLARE
  V_LOGID NUMBER;
  V_USER  VARCHAR(30);
  V_DATE  DATE;

BEGIN
  SELECT EVENTLOG_ID_SEQ.NEXTVAL, USER INTO V_LOGID, V_USER FROM DUAL;
  IF INSERTING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Time_done, Action_done)
    VALUES (V_LOGID, V_USER, SYSDATE, SYSDATE, 'INSERT');
  ELSIF DELETING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, SYSDATE, SYSDATE, 'DELETE');
  ELSIF UPDATING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, SYSDATE, SYSDATE, 'UPDATE');
  END IF;
END;
/

[![INSERT INTO 
EMPLOYEE_T (EmployeeID, EmployeeName, EmployeeAddress, EmployeeCity, EmployeeState, EmployeeZip, EmployeeBirthDate, EmployeeDateHired, EmployeeSupervisor)
VALUES (2, 'JACKIE', 'AFRICA', 'kenya', 'NO', 11285, TO_DATE('13-OCT-1950', 'DD-MM-YYYY'), TO_DATE('27-JUN-2014', 'DD-MM-YYYY'), 455789);][1]][1]

enter image description here

1 个答案:

答案 0 :(得分:1)

您发布的错误代码不完整TRIGGER

如果要将数据插入TIMESTAMP列,则需要插入兼容记录。仅插入时间部分没有用,因为您无法执行列上可能需要的直接date算术/比较操作。我在此处使用了SYSTIMESTAMP函数来插入当前系统TIMESTAMP

CREATE OR REPLACE TRIGGER SUPERHEROES_AUDIT
AFTER INSERT OR DELETE OR UPDATE ON SUPERHEROES
FOR EACH ROW
ENABLE
DECLARE
  V_LOGID NUMBER;
  V_USER  VARCHAR(30);
  V_DATE  DATE;

BEGIN
  SELECT EVENTLOG_ID_SEQ.NEXTVAL, USER, SYSDATE INTO V_LOGID, V_USER, V_DATE FROM DUAL;
  IF INSERTING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Time_done,Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, SYSTIMESTAMP, 'INSERT');
  ELSIF DELETING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done,Time_done, Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, SYSTIMESTAMP, 'DELETE');
  ELSIF UPDATING THEN
    INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done,Time_done, Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, SYSTIMESTAMP, 'UPDATE');
  END IF;
END;
/

以下是示例数据

select * FROM EVENTLOGS;


EVENTLOG_ID USER_NAME      DATE_DONE TIME_DONE                       ACTION_DONE 
----------- -------------- --------- ------------------------------- ------------
          1 SLDDEVELOPER   13-OCT-17 13-OCT-17 04.58.07.237343000 AM INSERT      

如果您只需要时间部分而不是DATE列中的time_done部分,那么它应该是VARCHAR2列,您可以在插入时使用TO_CHAR这种情况就像你试图做的那样。

INSERT INTO EVENTLOGS(Eventlog_id, User_name, Date_done, Time_done,Action_done)
    VALUES (V_LOGID, V_USER, V_DATE, TO_CHAR(SYSDATE,'HH24:MI:SS'), 'INSERT');