1

我有一个这样的查询:

select from_tz(to_timestamp(v_time,'YY-MM-DD HH24:MI:SS'), 'UTC') 
      at time zone 'America/New_York' from dual;

如果我使用字符串 Date 而不是 v_time 那么它工作正常。但是我需要在 to_timestamp 中传递一个变量 v_time,我该怎么做呢?

提前致谢。

完整代码在这里:

DECLARE
     v_test varchar2(200);
     v_cur_time varchar2(200);
     v_local_strem_time varchar2(200);
     v_time varchar2(200);
     v_time_diff NUMBER(20);
      v_temp varchar2(200);
     BEGIN

    SELECT TO_CHAR(SYSDATE, 'DD-MON-YY HH24:MI:SS') into v_cur_time
    FROM DUAL;

    select substr(((select TO_CHAR(date_time,'YY-MM-DD HH24:MI:SS') from observation_measurement where observation_measurement_id=5777992)), 1,17)
    into v_time  from dual;

    v_temp := v_time;
    select from_tz(to_timestamp(v_temp,'YY-MM-DD HH24:MI:SS'), 'UTC') 
    at time zone 'America/New_York' into v_local_strem_time from dual;

      select 24 * (to_date(v_cur_time, 'YY-MM-DD HH24:MI:SS') 
             - to_date(v_local_strem_time, 'YY-MM-DD HH24:MI:SS')) into v_time_diff 
       from dual;

       DBMS_OUTPUT.PUT_LINE(v_time_diff); 
    END;
4

1 回答 1

-1

变量必须是declared(在一个code block或一个procedure或一个function或等)

declare 
v_time varchar(20);
outval timestamp;
begin
v_time := '15-02-20 07:13:10';
select from_tz(to_timestamp(v_time,'YY-MM-DD HH24:MI:SS'), 'UTC') 
      at time zone 'America/New_York' into outval from dual;
dbms_output.put_line(outval);
end;

输出: 20-FEB-15 02.13.10.000000 AM

编辑后编辑问题: 问题在于v_local_strem_time包含AMERICA/NEW_YORK字符串,一种方法是使用该SUBSTR函数删除不必要的字符串。让我测试一下:


Create table observation_measurement(
observation_measurement_id number,
date_time date
)

insert into observation_measurement
values (5777992,to_date('214/10/09', 'YY-MM-DD HH24:MI:SS'));

DECLARE
     v_test varchar2(200);
     v_cur_time varchar2(200);
     v_local_strem_time varchar2(200);
     v_time varchar2(200);
     v_time_diff NUMBER(20);
     v_temp varchar2(200);
     BEGIN

    SELECT TO_CHAR(SYSDATE, 'DD-MON-YY HH24:MI:SS') into v_cur_time
    FROM DUAL;

    select substr(((select TO_CHAR(date_time,'YY-MM-DD HH24:MI:SS') from observation_measurement where observation_measurement_id=5777992)), 1,17)
    into v_time  from dual;

    v_temp := v_time;
    select from_tz(to_timestamp(v_temp,'YY-MM-DD HH24:MI:SS'), 'UTC') 
    at time zone 'America/New_York' into v_local_strem_time from dual;

      select 24 * (to_date(v_cur_time, 'YY-MM-DD HH24:MI:SS') 
             - to_date(substr(v_local_strem_time,0,18), 'YY-MM-DD HH24:MI:SS')) into v_time_diff 
       from dual;

       DBMS_OUTPUT.PUT_LINE(v_time_diff); 
    END;

输出: 143241

也可能observation_measurement.date_time您的结构的数据类型不是日期,那时您可能需要进行适当的转换。

于 2015-02-20T03:45:09.677 回答