INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Date variable not increasing in Function...

Date variable not increasing in Function...

(OP)
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 ;

RE: Date variable not increasing in Function...

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.

RE: Date variable not increasing in Function...

(OP)
Many thanks!

RE: Date variable not increasing in Function...

Blackhawks,

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

santaMufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close