• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

Extract year information from date string in oracle

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,
0
crgary_tx
Asked:
crgary_tx
1 Solution
 
schwertnerCommented:
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
   
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now