Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date variable not increasing in Function...

Status
Not open for further replies.

Blackhawks4Life

Programmer
Jul 9, 2013
3
0
0
US
I created the following function, and I pass in the year 2013. I expected the function to return '02-JAN-13', but it is still returning '01-JAN-13'. What am I missing?

I am using Toad for Oracle, on Oracle 11G. Thank you in advance for the assistance.

CREATE OR REPLACE FUNCTION COV_OBJECTS.GET_HOLIDAY_DATES ( P_YEAR IN INT ) RETURN VARCHAR2
IS
P_NEW_YEARS DATE;
BEGIN

SELECT TO_DATE('01-JAN-' || TO_CHAR(P_YEAR),'DD-MON-YYYY') INTO P_NEW_YEARS FROM DUAL;

IF UPPER(TO_CHAR(P_NEW_YEARS,'DAY')) = 'TUESDAY' THEN
SELECT P_NEW_YEARS + 1 INTO P_NEW_YEARS FROM DUAL;
END IF;

RETURN P_NEW_YEARS;

END ;
 
What you are missing are the trailing blanks returned from the TO_CHAR function.
Try this:
Code:
CREATE OR REPLACE FUNCTION GET_HOLIDAY_DATES ( P_YEAR IN NUMBER ) RETURN DATE 
IS
   P_NEW_YEARS DATE;
BEGIN
   p_new_years := TRUNC(TO_DATE('01-JAN-' || TO_CHAR(P_YEAR),'DD-MON-YYYY'));
   IF TRIM(TO_CHAR(P_NEW_YEARS,'DAY')) = 'TUESDAY' THEN
      p_new_years := p_new_years + 1;
   END IF;
   RETURN P_NEW_YEARS;
END ;
Also note that I eliminated the SELECTs - they cause needless context switching which will slow down your function.

The problem you were running into is that instead of 'TUESDAY', your TO_CHAR call was returning 'TUESDAY '. Thus, your comparison was not getting a match.
 
Blackhawks,

Be sure to click on the link "
star.gif
Like this post? Star It?
" at the bottom of Carp's helpful post.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top