[英]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' --inputdate的格式应为“ mm / dd / yyyy”,我需要将其转换为仅“ 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". //如果日期无效,该如何执行,即,如果接收到的日期格式不是mm / dd / yyyy格式,则必须给出msg,例如“给出了无效的输入”。
raise error;
Since your parameter inputdate
is a date
already, it can't be invalid * . 由于您的参数
inputdate
已经是date
,因此它不能无效* 。 You said: 你说:
In the table the value stored as 6/19/2007 10:05:00AM(mm/dd/yyyy format)
在表格中,该值存储为6/19/2007 10:05:00 AM(mm / dd / yyyy格式)
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. 当您在表中查询
date
值时,客户端将检索该内部表示并将其根据其设置或NLS_DATE_FORMAT转换为字符串,从而使您可以识别某些内容。
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. 在您的过程中,您正在调用
to_char(input_date)
,它将始终在有效日期上工作,因此您无需检查任何内容。 But as you don't use l_tempdate
it seems like a pointless check. 但是,由于您不使用
l_tempdate
因此似乎毫无意义。
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.
您收到一个日期,根据定义,该日期可以是有效日期,也可以是NULL。 No need to check anything (except for NULL, if you want so).
无需检查任何内容(如果需要,可以检查NULL)。 You can display a date in any format you like, be it 'mm/yyyy' or anything else.
您可以按自己喜欢的任何格式显示日期,例如“ mm / yyyy”或其他任何形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.