I wrote a function for stripping Dates

I wrote this
 if UPPER(p_string) = 'D' then
    SELECT extract(DAY From TO_DATE(dt, 'dd-MON-yy')) into v_new_key FROM dual;
    if LENGTH(v_new_key) = 1 then
      v_new_key := LPAD(v_new_key,2,'0');
    end if;
  elsif UPPER(p_string) = 'M' then
    SELECT extract(MONTH From TO_DATE(dt, 'dd-MON-yy')) into v_new_key FROM dual;
    if LENGTH(v_new_key) = 1 then
      v_new_key := LPAD(v_new_key,2,'0');
    end if;
  elsif UPPER(p_string) = 'Y' then
    SELECT extract(YEAR From TO_DATE(dt, 'dd-MON-yy')) into v_new_key FROM dual;
  else
    v_new_key := '0';
  end if;
   RETURN v_new_key;

It takes way too long. Does anyone have any ideas of how I can speed this up?
jknj72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
RETURN CASE UPPER(p_string)
               WHEN 'D' THEN TO_CHAR(TO_DATE(dt, 'dd-MON-yy'), 'dd')
               WHEN 'M' THEN TO_CHAR(TO_DATE(dt, 'dd-MON-yy'), 'mm')
               WHEN 'Y' THEN TO_CHAR(TO_DATE(dt, 'dd-MON-yy'), 'yyyy')
               ELSE '0'
           END;


While my version should be faster because it's not switching between sql and pl/sql,  it's unlikely you'll be able to see the difference unless you run it many, many times.


What makes you think this particular piece of code is a performance problem?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jknj72Author Commented:
Well I was running it for every loop and I was refreshing the table I was inserting into every once in a while and it was not populating very fast at all.

I tried your code and it seems to be running better so thanks
0
jknj72Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.