如何在更新查询中使用SUBSTR

时间:2018-05-14 07:43:20

标签: sql oracle sql-update substring

我在working_hour列时间中有以下更新查询 +000000000 01:14:00.771000000 此格式但我想显示时间01:14:00格式仅用于尝试使用SUBSTR但它给了我ORA-00927:缺少等号错误 这是我的查询

UPDATE  EMPLOYEE_LOGIN_TIME T1 SET SUBSTR(T1.WORKING_HOUR,12,8)=
(SELECT TO_TIMESTAMP (TO_CHAR(TIME_OUT))- TO_TIMESTAMP (TO_CHAR(TIME_IN)) AS WORKING_HOUR 
FROM EMPLOYEE_LOGIN_TIME T2 WHERE TRUNC(PUNCH_DATE)=TRUNC(SYSDATE)) WHERE T1.EMPLOYEE_ID='73584' AND TRUNC(T1.PUNCH_DATE)=TRUNC(SYSDATE)

我的表格结构如下:

TIME_IN         TIMESTAMP(6) WITH LOCAL TIME ZONE
TIME_OUT            TIMESTAMP(6) WITH LOCAL TIME ZONE
WORKING_HOUR        VARCHAR2(30 BYTE)
PUNCH_DATE          DATE
TIME_OUT_HISTORY    VARCHAR2(200 BYTE)

1 个答案:

答案 0 :(得分:0)

TIMESTAMP - TIMESTAMP提供INTERVAL DAY TO SECOND结果(不是字符串) - 只需将其添加到纪元数据中,然后使用TO_CHAR获取时间组件:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE EMPLOYEE_LOGIN_TIME(
  EMPLOYEE_ID         INTEGER,
  TIME_IN             TIMESTAMP(6) WITH LOCAL TIME ZONE,
  TIME_OUT            TIMESTAMP(6) WITH LOCAL TIME ZONE,
  WORKING_HOUR        VARCHAR2(30 BYTE),
  PUNCH_DATE          DATE,
  TIME_OUT_HISTORY    VARCHAR2(200 BYTE)
);

INSERT INTO EMPLOYEE_LOGIN_TIME ( 
  EMPLOYEE_ID,
  TIME_IN,
  TIME_OUT,
  WORKING_HOUR,
  PUNCH_DATE,
  TIME_OUT_HISTORY
)
SELECT 73584,
       SYSTIMESTAMP,
       SYSTIMESTAMP + INTERVAL '+000000000 01:14:00.771000000' DAY TO SECOND,
       NULL,
       SYSDATE,
       NULL
FROM   DUAL;

查询1

UPDATE  EMPLOYEE_LOGIN_TIME
SET WORKING_HOUR = TO_CHAR( 
                     DATE '0001-01-01' + ( TIME_OUT- TIME_IN ),
                     'HH24:MI:SS'
                   )
WHERE  EMPLOYEE_ID= 73584
AND    PUNCH_DATE >= TRUNC(SYSDATE)
AND    PUNCH_DATE <  TRUNC(SYSDATE) + INTERVAL '1' DAY;

查询2

SELECT EMPLOYEE_ID,
       TO_CHAR( TIME_IN,  'YYYY-MM-DD HH24:MI:SS.FF6 TZR' ) AS TIME_IN,
       TO_CHAR( TIME_OUT, 'YYYY-MM-DD HH24:MI:SS.FF6 TZR' ) AS TIME_OUT,
       WORKING_HOUR,
       PUNCH_DATE,
       TIME_OUT_HISTORY
FROM   EMPLOYEE_LOGIN_TIME;

<强>结果:

| EMPLOYEE_ID |                        TIME_IN |                       TIME_OUT | WORKING_HOUR |          PUNCH_DATE | TIME_OUT_HISTORY |
|-------------|--------------------------------|--------------------------------|--------------|---------------------|------------------|
|       73584 | 2018-05-14 09:00:00.000000 PST | 2018-05-14 10:14:00.771000 PST |     01:14:00 | 2018-05-14 09:00:00 |           (null) |