简体   繁体   中英

how do i validate the date fomat as input(MM/DD/YYYY) as valid,invalid and null with out using any functions in pl/sql(by using only if else)

--inputdate should be in the format of 'mm/dd/yyyy' which i needs to convert it to only 'mm/yyyy'

procedure validatedate(inputdate date)

declare
l_tmpdate varchar2(20);

BEGIN

  if inputdate is NOT NULL then
   BEGIN
   l_tmpdate:=to_char(inputdate ,'mm/yyyy');
   end;
   else
     dbms_output.put_line('Mandatory input should be given--Date should not be NULL');
     RAISE application_error;

//How to perform if the date is invalid ie, if the date format is received other than mm/dd/yyyy format it has to give the msg like "Invalid input is given".

   raise error;

Since your parameter inputdate is a date already, it can't be invalid * . You said:

In the table the value stored as 6/19/2007 10:05:00AM(mm/dd/yyyy format)

Dates are stored internally as numbers , they are not formatted. When you query your table for a date value the client retrieves that internal representation and converts it to a string based on its settings or your NLS_DATE_FORMAT, giving you something recognisable.

In your procedure you are calling to_char(input_date) , which will always be working on a valid date, so there is nothing for you to check. But as you don't use l_tempdate it seems like a pointless check.

It would be a different matter if you were passing a string into a procedure and converting that to a date. Then you would need to allow for and handle invalid inputs, unless you knew you could guarantee you were always going to be passed valid values - if it's only ever called from an application layer that did the validation, for example. Nothing is foolproof though.

* Well, usually; it's possible to create invalid dates but it's hard work, and almost certainly not something you need to worry about here.

You don't receive any date format. You receive a date, which is either a valid date or NULL by definition. No need to check anything (except for NULL, if you want so). You can display a date in any format you like, be it 'mm/yyyy' or anything else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM