简体   繁体   English

ORA-06502:PL / SQL:数字或值错误:Oracle SQL查询中的字符串缓冲区太小

[英]ORA-06502: PL/SQL: numeric or value error: character string buffer too small in Oracle sql query

I want to calculate the time difference between two dates in Oracle sql query. 我想计算Oracle sql查询中两个日期之间的时差。 I have written the following query: 我写了以下查询:

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;

The first select statement returns correct result. 第一条选择语句返回正确的结果。 When I am trying to calculate the time difference from current date to the previous calculated date then it is showing error. 当我尝试计算从当前日期到上一个计算日期的时差时,它显示错误。 How can I get the correct result? 如何获得正确的结果?

Thanks!!!! 谢谢!!!!

Increasing the size of diff_hours variable working. 增加diff_hours变量工作的大小。 Thanks Krishna. 谢谢克里希纳。 :-) :-)

Make diff_hours a NUMBER variable instead of a VARCHAR2 variable! diff_hours设为NUMBER变量而不是VARCHAR2变量!

Of course you can make diff_hours long enough to hold all the insignificant decimals your query produces, but declaring it VARCHAR2 is pointless anyway! 当然,您可以使diff_hours足够长,以容纳查询产生的所有无关紧要的小数,但是将其声明为VARCHAR2毫无意义!

Hi I have made some corrections in your query . 嗨,我在您的查询中做了一些更正。 Tell me if that works 告诉我是否可行

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;

You can increase diff_hours and it might work in some cases, but the problem is it might not work in other cases. 您可以增加diff_hours,在某些情况下可能会起作用,但问题是在其他情况下可能会不起作用。

To be sure, you need explisitly convert number to string (example with 2 digits after dot): 可以肯定的是,您需要将数字明确转换为字符串(例如,在点后加2位数字):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Oracle数据库错误:ORA-06502:PL / SQL:数字或值错误:字符串缓冲区太小 - Oracle Database Error: ORA-06502: PL/SQL: numeric or value error: character string buffer too small Oracle SQL 错误代码 ORA-06502: PL/SQL: numeric or value error: string buffer too small at line 18 - Oracle SQL Error Code ORA-06502: PL/SQL: numeric or value error: character string buffer too small at line 18 ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小 - ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06502:PL/SQL:数字或值错误:字符串缓冲区太小,只有三个数字 - ORA-06502: PL/SQL: numeric or value error: character string buffer too small only three numbers ORA-06502: PL/SQL: 数字或值错误: 字符串缓冲区太小: 构建时 model - ORA-06502: PL/SQL: numeric or value error: character string buffer too small: When building a model tab_to_string [错误]执行(37:13):ORA-06502:PL / SQL:数字或值错误:字符串缓冲区太小 - tab_to_string [Error] Execution (37: 13): ORA-06502: PL/SQL: numeric or value error: character string buffer too small SQL错误:ORA-06502:PL / SQL:数字或值错误 - SQL Error: ORA-06502: PL/SQL: numeric or value error 奇怪的ORA-06502:PL / SQL:数字或值错误 - Strange ORA-06502: PL/SQL: numeric or value error ORA-06502: PL/SQL: 连接时出现数字或值错误 - ORA-06502: PL/SQL: numeric or value error when concatenating 获取ORA-06502:PL / SQL:数字或值错误:SQL触发器中的字符到数字转换错误 - Getting ORA-06502: PL/SQL: numeric or value error: character to number conversion error in SQL trigger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM