将日期与预定义格式 pl sql 进行比较

2021-12-24 00:00:00 oracle plsql

我有一个函数,它应该接受 DD-MON-YY 格式的日期,并且应该以 DD-MM-YYYY 格式显示.我创建的函数是:

I have a function which should accept date in a format DD-MON-YY and should display in the format DD-MM-YYYY. The function I have created is :

create or replace function PRINT_IT(abc date)
RETURN DATE
IS
  v_date DATE;
BEGIN
  if regexp_like(abc,'^[0-9]{2}-[a-z]{3}-[0-9]{2}$')
  then
    v_date := TO_DATE(abc,'DD-MM-YYYY');
  else
    dbms_output.put_line('wrong format');
  end if;
  dbms_output.put_line('The date is ');
  return v_date;
END PRINT_IT;

但是返回的值总是错误的日期格式!!

but the value returned is always wrong date format!!

推荐答案

不,不是.您的日期以您的 NLS_DATE_FORMAT<指定的格式输出/a>.如果不同,我想显示然后为您的会话更改此参数:

No, it's not. Your date is being output in the format specified by your NLS_DATE_FORMAT. I you want to display if differently then change this parameter for your session:

alter session set nls_date_format = 'dd-mm-yyyy'

注意显示这个词.这就是所有这些.这就是你应该考虑做的所有事情.日期的显示方式绝不会影响它的存储方式.

Note the word display. That's all this does. That's all you should consider doing. The way a date is displayed in no way effects the way it is stored.

通常情况下,您可能会使用 TO_CHAR() 使用适当的格式模型来显示日期,即 to_char(my_date, 'dd-mm-yyyy').它将不再是一个日期而是一个字符.

More normally you might use TO_CHAR() with an appropriate format model to display a date, i.e. to_char(my_date, 'dd-mm-yyyy'). It will no longer be a date but a character.

您似乎不想像您所说的那样显示日期.您正在从函数中返回值,在这种情况下,我会坚持使用您拥有的值.将日期从数据库中取出时,只需将其转换为合适的格式即可显示,始终将其作为日期存储在数据库中.这反过来意味着它存储在数据库中时的样子并不重要,只要它实际上是一个日期即可.

It doesn't look like you want to display a date as you've said. You're returning the value from your function, in which case I would stick with what you have. You only need to transform a date into an appropriate format for display when taking it out of the database, always store it as a date in the database. This in turn means that it doesn't matter what it looks like when stored in the database, merely that it is actually a date.

相关文章