We help IT Professionals succeed at work.

Extract year information from date string  in oracle

crgary_tx
crgary_tx asked
on
Medium Priority
644 Views
Last Modified: 2012-05-06
I have a field say dat_val in the table of type char. The dates are stored in two different formats as of now. ie yyyy-mm-dd and mm-dd-yyyy. I am having problem writing a function to extract the Year from this string field. The function should be able to extract year anywhere in the string along with the above two formats, say mm-yyyy-dd!

Thanks,
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2008
Commented:
CREATE OR  REPACE FUNCTION extr (p_dt VARCHAR2) RETURN VARCHAR2
v_pos   INTEGER;
BEGIN
  v_pos := INSTR(p_dt,'-');
  IF      v_pos = 5   THEN RETURN (substr(p_dt,1,4)
  ELSE RETURN (substr(p_dt,7,4)
  END IF;
END;
/
show err
   

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.