PL/SQL check date is valid

前端 未结 2 525
囚心锁ツ
囚心锁ツ 2021-01-15 12:18

If I have a date format DDMM in PL/SQL and I want to validate it. What is the correct way to do it?

DD is the day and MM is th

2条回答
  •  执笔经年
    2021-01-15 12:48

    to_date raises an exception if its input parameter is not a valid date. So you can do something like:

    declare
      x date;
    begin
      x := to_date('3210', 'DDMM'); -- Will raise ORA-1847
      x := to_date('0113', 'DDMM'); -- Will raise ORA-1843
    exception
      when others then 
        if sqlcode in (-1843, -1847) then
          dbms_output.put_line('Invalid Date!');
          null;
        else
          raise;
        end if;
    end;
    

提交回复
热议问题