简体   繁体   English

我如何在不使用pl / sql中的任何函数的情况下验证日期格式作为输入(MM / DD / YYYY)的有效,无效和null(仅在其他情况下使用)

[英]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.

相关问题 如何在 PL/SQL 中将日期格式从 MM/DD/YYYY 更改为 YYYY-MM-DD? - How to change the date format from MM/DD/YYYY to YYYY-MM-DD in PL/SQL? 使用T-SQL如何将日期格式mmm-dd-yyyy转换为yyyy-mm-dd - Using T-SQL, how to convert date format mmm-dd-yyyy to yyyy-mm-dd 如何使用 sql server 2008 r2 将日期显示为 mm/dd/yyyy hh:mm Am/PM? - How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2? 使用Google BigQuery在SQL中的订购日期(dd-mm-yyyy) - Ordering date (dd-mm-yyyy) in SQL using Google BigQuery 使用 oracle 数据读取器我无法仅使用 ToString("dd/MM/yyyy") 将日期时间格式化为日期 - using oracle data reader i can't format datetime into date only using ToString("dd/MM/yyyy") 如何使用 SQL 按格式 dd/mm/yyyy 将日期字符串 12012017 转换为 DATE - How to Convert Date String 12012017 to DATE using SQL by format dd/mm/yyyy 如何使用SQL在MM / DD / YYYY HH:MM中减去2个字符串类型时间? - How can I subtract 2 string type times in MM/DD/YYYY HH:MM using SQL? 如何在bigquery中将字符串yyyy / mm / dd转换为日期? - How do I convert a string yyyy/mm/dd to date in bigquery? 如何将 SQL 日期结果转换为英国格式 (dd/mm/yyyy) - How do I convert an SQL date result to British format (dd/mm/yyyy) 如何使用SQL将日期格式转换为YYYY-MM-DD - How do I convert date format to YYYY-MM-DD with SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM