[英]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.