【问题标题】:ORA-06502: PL/SQL: numeric or value error: character string buffer too small in Oracle sql queryORA-06502: PL/SQL: 数字或值错误: Oracle sql 查询中的字符串缓冲区太小
【发布时间】:2014-06-25 13:05:42
【问题描述】:

我想在 Oracle sql 查询中计算两个日期之间的时间差。我写了以下查询:

DECLARE
 v_time varchar2(40); 
 diff_hours varchar2(40); 
 BEGIN
 select substr(((select date_time from observation_measurement where observation_measurement_id=2861971)), 1,17)
 into v_time
 from dual;

 dbms_output.put_line(v_time);

 select 24 * (to_date('06-25-2014 09:46:36', 'MM-DD-YYYY hh24:mi:ss') 
             - to_date(v_time, 'YY-MM-DD hh24:mi:ss')) into diff_hours 
       from dual;
END;

第一个 select 语句返回正确的结果。当我试图计算从当前日期到上一个计算日期的时间差时,它会显示错误。我怎样才能得到正确的结果?

谢谢!!!!

【问题讨论】:

  • 你在增加 diff_hours 变量的大小后尝试过吗?
  • 太棒了。增加 diff_hours 工作的大小。我没有注意到这一点。谢谢:-)

标签: sql oracle date difference


【解决方案1】:

增加 diff_hours 变量的大小。谢谢克里希纳。 :-)

【讨论】:

    【解决方案2】:

    diff_hours 设为NUMBERvariable 而不是VARCHAR2 变量!

    当然,您可以使diff_hours 足够长,以容纳您的查询产生的所有微不足道的小数,但无论如何声明它VARCHAR2 毫无意义!

    【讨论】:

      【解决方案3】:

      您好,我对您的查询进行了一些更正。告诉我这是否有效

      DECLARE
       v_time varchar2(40);
      -- Increase the size  
       diff_hours varchar2(200); 
       BEGIN
       select substr(((select date_time from observation_measurement where observation_measurement_id=2861971)), 1,17)
       into v_time
       from dual;
      
       dbms_output.put_line(v_time);
      -- using the same format mask for both the dates
       select 24 * (to_date('06-25-2014 09:46:36', 'MM-DD-YYYY hh24:mi:ss') 
                   - to_date(v_time, 'MM-DD-YYYY hh24:mi:ss')) into diff_hours 
             from dual;
      END;
      

      【讨论】:

        【解决方案4】:

        您可以增加 diff_hours,它在某些情况下可能会起作用,但问题是它在其他情况下可能不起作用。

        可以肯定的是,您需要明确地将数字转换为字符串(点后有 2 位的示例):

        select trunc(24*...., 2) into diff_hours
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-14
          • 2011-03-31
          • 2011-11-25
          • 1970-01-01
          • 2018-03-16
          • 2015-05-16
          • 1970-01-01
          相关资源
          最近更新 更多