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!!
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:
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.
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.