Hi I have the following function created in Oracle.
i will have to pass date-1 and date-2 parameters into the function and the function should return the another date to me .
please see the code below.
create or replace function GETD(p_d1 in date,
p_d2 in date ) return DATE
as
l_result DATE;
begin
SELECT EDIT_Date into l_result FROM qa.employees WHERE qa.employee_join_date BETWEEN TO_DATE(p_d1, 'MM/DD/YYYY') AND TO_DATE(p_d2, 'MM/DD/YYYY') AND ROWNUM <= 1
;
return l_result;
end;
i execute the function as below
SELECT GETD('27-JUN-12','28-JUN-12') FROM DUAL
the function get compiled and while im passing the parameters to execute the function i get the following error “Not a Valid Month”.
Could some one please tell me where im going wrong
thanks
Justin
First, you should not call
TO_DATEon a date variable. If you do that, you force Oracle to first convert the date to a string using the session’sNLS_DATE_FORMATand then convert the string back to a date using the specified format mask. If yourNLS_DATE_FORMAThappens not to match the specified format mask (and, since theNLS_DATE_FORMATis controlled by the client, some users and sessions will inevitably have a different date format), you’ll get an error.Assuming your intention was merely to ignore any time component, you should be using the
truncfunction instead.The presence of a
rownum <= 1condition seems a bit odd– it seems unlikely that you really want to fetch an arbitrary row from the table that matches theemployee_join_datecriteria. Not knowing the requirements, it seems likely that you want to do something deterministic that fetches the row with the minimum (or maximum)edit_date.Second, when you call the function, you should pass in
DATEparameters rather than passing in strings and letting Oracle implicitly convert the strings to dates using the session’sNLS_DATE_FORMAT. If you use date literalsor convert the strings to dates explicitly using a
TO_DATEthen your call will work regardless of the client’s
NLS_DATE_FORMAT.