1
undefine dates

declare
  v_dateInput VARCHAR(10);
  v_dates DATE;
begin
  v_dateInput := &&dates;
  v_dates := to_date(v_dateInput,'dd-mm-yyyy');
  DBMS_OUTPUT.put_line(v_dates);
end;

不知道为什么每当我使用例如输入 03-03-1990 运行此代码时,都会出现此错误。

Error report:
ORA-01847: day of month must be between 1 and last day of month
ORA-06512: at line 6
01847. 00000 -  "day of month must be between 1 and last day of month"
*Cause:    
*Action:
4

2 回答 2

12

哈,不错。那是因为 && 就地替换了变量,所以你的脚本变成了:

declare
  v_dateInput VARCHAR(10);
  v_dates DATE;
begin
  v_dateInput := 03-03-1990;
  v_dates := to_date(v_dateInput,'dd-mm-yyyy');
  DBMS_OUTPUT.put_line(v_dates);
end;

注意没有引号。v_dateInput 实际上是 '-1990',因为 oracle 计算 03 - 03 - 1990 的数值。当然,这不适用于给定的格式字符串。

要修复它,您需要

v_dateInput := '&&dates';
于 2010-03-25T12:10:17.810 回答
1

当提示输入日期值时,请确保将其放在单引号中,例如

Enter value for dates: '03-03-1990'

分享和享受。

于 2010-03-25T12:19:45.297 回答