我在SQL Developer中具有以下脚本,但是在SQL Plus命令行中运行该脚本时似乎返回了无效的日期。
set serveroutput on;
DECLARE
yesterday varchar2(30);
tz_yesterday varchar2(30);
BEGIN
select sysdate - 1 into yesterday from dual;
select cast(yesterday as timestamp WITH LOCAL TIME ZONE) into tz_yesterday from dual;
dbms_output.put_line(yesterday);
dbms_output.put_line(tz_yesterday);
END;
这是SQL Developer提供的输出,它是正确的:
10-OCT-18
10-OCT-18 12.00.00.000000 AM
这就是我在SQL Plus中获得的东西
SQL> l
1 DECLARE
2 g_vendor_id NUMBER;
3 g_product_id NUMBER;
4 yesterday varchar2(30);
5 tz_yesterday varchar2(30);
6 g_user_id NUMBER;
7 BEGIN
8 select sysdate - 1 into yesterday from dual;
9 select cast(yesterday as timestamp WITH LOCAL TIME ZONE) into tz_yesterday from dual;
10 dbms_output.put_line(yesterday);
11 dbms_output.put_line(tz_yesterday);
12* End;
SQL> /
DECLARE
*
ERROR at line 1:
ORA-01843: not a valid month
ORA-06512: at line 9
有人可以在这里解释差异吗?
答案 0 :(得分:4)
不要将日期或时间戳记值存储在varchar变量中,请始终使用正确的数据类型:
set serveroutput on;
DECLARE
yesterday date;
tz_yesterday timestamp with local time zone;
BEGIN
yesterday := sysdate - 1;
tz_yesterday := cast(yesterday as timestamp WITH LOCAL TIME ZONE);
dbms_output.put_line(yesterday);
dbms_output.put_line(tz_yesterday);
END;
/