截止日期,无格式警告

编译代码后,我得到一些需要摆脱的警告/提示。第一个是To_date,没有格式问题

_current_end := to_date(((to_number(yearStatus)) ||
                                          MonthAndDay),'YYYYMMDD');

我收到错误信息“ Procedure_Name中没有格式的提示TO_DATE”,但我认为YYYYMMDD会删除它。

有什么想法吗?

qingfeng710 回答:截止日期,无格式警告

有2个多余的括号:

这个小块可以正常工作:

Declare
  yearStatus   varchar2(4):='2019';
  MonthAndDay  varchar2(4):='1101';
  current_end  date;
begin
  current_end:=to_date(to_number(yearStatus)||MonthAndDay,'YYYYMMDD'
                       );
  Dbms_Output.Put_Line('current_end='||to_char(current_end,'yyyy-mm-dd hh24:mi:ss'));
End;
/

输出为:

PL/SQL block,executed in 62 ms
current_end=2019-11-01 00:00:00
Total execution time 343 ms
,

多于不必要的括号并不能使事情变得更好。 PL / SQL不是Lisp(括号中多余的也是不好的:-)-您不必用括号将每个函数调用括起来。另外,IMO太多空格也是如此。

current_end := TO_DATE(TO_NUMBER(yearStatus) || MonthAndDay,'YYYYMMDD');

至少在我看来更容易阅读和理解。

dbfiddle here

,

括号没有什么问题(是的,您可以删除一些括号,但是将其保留在其中并不是无效的语法);但是,在PL / SQL中不能有以下划线开头的变量。

DECLARE
  yearStatus  CHAR(4) := '2019';
  MonthAndDay CHAR(4) := '0101';
  current_end DATE;
BEGIN
  current_end := to_date(((to_number(yearStatus)) ||
                                          MonthAndDay),'YYYYMMDD');
  DBMS_OUTPUT.PUT_LINE( current_end );
END;
/

输出:

2019-01-01T00:00:00

但是,如果您想简化代码,则可以摆脱TO_NUMBER调用,因为||的串联运算符将隐式地将其重新铸造为字符串:

DECLARE
  yearStatus  CHAR(4) := '2019';
  MonthAndDay CHAR(4) := '0101';
  current_end DATE;
BEGIN
  current_end := to_date(yearStatus || MonthAndDay,'YYYYMMDD');
  DBMS_OUTPUT.PUT_LINE( current_end );
END;
/

db 提琴here

,

实际上,您可以一起删除字符串连接。

alter session set nls_date_format = 'yyyy/mm/dd"T"hh24:mi:ss';
declare
  yearstatus  char(4) := '2019';
  current_end date;
begin
  current_end := trunc(to_date(yearstatus,'yyyy'),'yyyy');
  dbms_output.put_line( current_end );
end;

有没有更好的选择?您的选择。

本文链接:https://www.f2er.com/3142103.html

大家都在问