Skip to Main Content
  • Questions
  • Converting VARCHAR2 date string to ‘mm/dd/yyyy'

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mallesh.

Asked: September 30, 2015 - 9:28 pm UTC

Last updated: October 01, 2015 - 6:18 am UTC

Version: 11g

Viewed 10K+ times! This question is

You Asked

Hi,

We have a VARCHAR2 column which has date strings in different formats. we need it to convert it to single format 'mm/dd/yyyy' in the queries for further usage in application. What is the best way to do it.

Different date formats in VARCHAR2 column.
eg: 1) JUL 05, 2015
2) 03/26/2007

Thanks in advance !!

and Connor said...

Oracle will automatically try a number of ways to work out the date even if it is NOT your standard format, for example

SQL> select to_date('01@jan@12') from dual;

TO_DATE('
---------
01-JAN-12


but you can also create a function to try an assortment of different masks. Here's one that you can edit to handle whatever masks suit you

SQL> create or replace
  2  function date_tester(p_str varchar2) return date is
  3    type format_masks is table of varchar2(20);
  4    l_mask format_masks
  5     := format_masks(
  6         'dd/mm/yyyy',
  7         'dd-mon-yy',
  8         'dd/mon/rr',
  9         'dd@mm@yy',
 10         'dd-mon@yy',
 11         'ddmmyy',
 12         'ddmonyy'
 13         );
 14    l_date date;
 15  begin
 16    for i in 1 .. l_mask.count
 17    loop
 18      begin
 19        l_date := to_date(p_str,l_mask(i));
 20        return l_date;
 21      exception
 22        when others then null;
 23      end;
 24    end loop;
 25    return null;
 26  end;
 27  /

Function created.

SQL>
SQL> select date_tester('01-jan-12') from dual;

DATE_TEST
---------
01-JAN-12

SQL> select date_tester('01-jan@12') from dual;

DATE_TEST
---------
01-JAN-12

SQL> select date_tester('01jan12') from dual;

DATE_TEST
---------
01-JAN-12

SQL> select date_tester('010012') from dual;

DATE_TEST
---------



Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thank you !

Mallesh Nagarajan, October 01, 2015 - 1:09 am UTC

Thank you !! It was very useful and helped me a lot.

date format

Ravikanth Beevanapalli, October 01, 2015 - 4:36 am UTC

How do you determine this value?

01/02/2001

Is it 1st Jan 2001 or 2nd Feb 2001?
Connor McDonald
October 01, 2015 - 6:18 am UTC

You can't - you need to make a choice in terms of priority, or throw an error - but there's always that risk if you want to handle both dd.mm and mm.dd

Why leave out the century part in an example ?

Hoek, October 01, 2015 - 9:43 am UTC

First of all, hats off to you and Chris for keeping AskTom alive while Tom can focus on his well-deserved time-out, Connor!

One thing though: I am not fond of examples regarding DATE-related questions that use a format containing only the year part and leave out the century part.
Especially nowadays, almost 16 years after Y2K.

"toms rule #2: stop using YY, just stop -- now, forever!!!. Always use to_date and
ALWAYS use a 4 character year mask. Just do it! You will never be sorry you did. You
WILL be sorry if you do not. (i still cannot believe we did not all learn this painful
lesson in 2000!)"
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2858890655722

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library