Home » SQL & PL/SQL » SQL & PL/SQL » Need to convert into date for differernt kind of formates (Oracle 11g )
Need to convert into date for differernt kind of formates [message #647276] Mon, 25 January 2016 04:16 Go to next message
saipradyumn
Messages: 419
Registered: October 2011
Location: Hyderabad
Senior Member
Hi

Please help me to convert into date for the folloinwg inut character formates by using regular expression .

with data as
 (select 'August 2015' v
    from dual
  union all
  select '2014 may' v
    from dual
  union all
  select 'June 2013' v from dual
  
  )
select * from data


Re: Need to convert into date for differernt kind of formates [message #647277 is a reply to message #647276] Mon, 25 January 2016 04:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Regular expressions takes a string and other parameters and return a string.
So regular expressions can't convert into a date, only TO_DATE can.
Database SQL Reference gives the description of the function and examples. Don't forget to click on the links in this page.

Re: Need to convert into date for differernt kind of formates [message #647300 is a reply to message #647276] Mon, 25 January 2016 14:04 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
The first thing that comes to mind is why you have data in various formats. If this is user input, then the input should be restricted to one format and clearly labeled. If this is some unformatted legacy data, then you do what you can with it. It is impossible to account for all possible formats, as, for example, there is no way to tell if 01-02-2016 is January second or the first of February. If you have a limited number of formats, then you can write a function to test for them. In the following example, the fix_date function first attempts to apply the to_date function using your most common format. If that format fails, then it tries the other one. If it fails both, then it returns null. You could modify it to attempt various other formats. The following also only uses one language, but you could modify it to use others. You can then insert your date into a date column, or display it in any format you wish using to_char.

SCOTT@orcl> create or replace function fix_date
  2    (p_string	 in varchar2)
  3    return		    date
  4  as
  5    v_date		    date;
  6    e_invalid_month	exception;
  7    pragma exception_init (e_invalid_month, -01843);
  8  begin
  9    begin
 10  	 v_date := to_date (lower (p_string), 'month yyyy', 'NLS_DATE_LANGUAGE = American');
 11    exception
 12  	 when e_invalid_month then
 13  	   begin
 14  	     v_date := to_date (lower (p_string), 'yyyy mon', 'NLS_DATE_LANGUAGE = American');
 15  	   exception
 16  	     when others then
 17  	     v_date := to_date (null);
 18  	   end;
 19  	 when others then
 20  	   v_date := to_date (null);
 21    end;
 22    return v_date;
 23  end fix_date;
 24  /

Function created.

SCOTT@orcl> show errors
No errors.
SCOTT@orcl> with data as
  2   (select 'August 2015' v
  3  	 from dual
  4    union all
  5    select '2014 may' v
  6  	 from dual
  7    union all
  8    select 'June 2013' v from dual
  9    )
 10  select to_char (fix_date (v), 'dy dd-mon-yyyy') fixed_date from data
 11  /

FIXED_DATE
---------------------------------
sat 01-aug-2015
thu 01-may-2014
sat 01-jun-2013

3 rows selected.

[Updated on: Mon, 25 January 2016 14:07]

Report message to a moderator

Re: Need to convert into date for differernt kind of formates [message #647338 is a reply to message #647300] Tue, 26 January 2016 15:53 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
One other note. You should never use a character string to store a date, always use a date column in the oracle table
Re: Need to convert into date for differernt kind of formates [message #647692 is a reply to message #647338] Fri, 05 February 2016 09:34 Go to previous messageGo to next message
suwrna
Messages: 4
Registered: May 2015
Junior Member
hi friends

1)What's the diff between them ? to_char(' 12-JUN-2011', 'dd-mm-yy') and to_date('12-JUN-2011','dd-mm-yy') ??
2) select to_date('25-DEC-10', 'DD-MON-YYYY') from dual; it works properly
But the following statement throw error
select to_char('25-DEC-10', 'DD-MON-YYYY') from dual;
error : ora :01722 :invalid number ?
why the second query (select to_char('25-DEC-10', 'DD-MON-YYYY') from dual;) give error
please explain. i confuse about these ,
Re: Need to convert into date for differernt kind of formates [message #647693 is a reply to message #647692] Fri, 05 February 2016 09:37 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
to_char(' 12-JUN-2011', 'dd-mm-yy') Means to convert the STRING ' 12-jun-2011' to a date using the default convert string for the database and then convert it back to a string. If you want to convert a string to another date string do the following

select to_char(to_date('12-JUN-2011','dd-mon-yyyy'),'dd-mm-yy') from dual;

[Updated on: Fri, 05 February 2016 09:37]

Report message to a moderator

Re: Need to convert into date for differernt kind of formates [message #647694 is a reply to message #647692] Fri, 05 February 2016 09:39 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
NEVER use TO_CHAR() on any string!
You can & should use TO_CHAR() to convert NUMBER or DATE to a string.

'12-JUN-2011' is already a character string & requires no datatype conversion.

TO_DATE() converts string to DATE datatype
Re: Need to convert into date for differernt kind of formates [message #647695 is a reply to message #647692] Fri, 05 February 2016 09:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

In the end, when else failed, read Database SQL Reference

Re: Need to convert into date for differernt kind of formates [message #647696 is a reply to message #647300] Fri, 05 February 2016 10:01 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3310
Registered: January 2010
Location: Connecticut, USA
Senior Member
Or:

with data as
 (select 'August 2015' v
    from dual
  union all
  select '2014 may' v
    from dual
  union all
  select 'June 2013' v from dual
)
select to_date(regexp_substr(v,'[[:alpha:]]+') || regexp_substr(v,'\d+'),'monthyyyy') dt
  from data
/

DT
---------
01-AUG-15
01-MAY-14
01-JUN-13

SQL> 


SY.
Re: Need to convert into date for differernt kind of formates [message #647698 is a reply to message #647276] Fri, 05 February 2016 10:20 Go to previous messageGo to next message
suwrna
Messages: 4
Registered: May 2015
Junior Member
select to_char('25-DEC-16') from dual;

result is 25-DEC-16 it working

but
select to_char('25-DEC-16', 'DD-MON-YY') from dual;
its throw error ?

parameter (input) is same, so why oracle throw error
Re: Need to convert into date for differernt kind of formates [message #647699 is a reply to message #647698] Fri, 05 February 2016 10:24 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
because you are trying to force an implicint conversion of '25-dec-16' to a date and then convert it to a string. you MUST use to_date, not to_char
Re: Need to convert into date for differernt kind of formates [message #647700 is a reply to message #647698] Fri, 05 February 2016 10:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
suwrna wrote on Fri, 05 February 2016 17:20
select to_char('25-DEC-16') from dual;

result is 25-DEC-16 it working

but
select to_char('25-DEC-16', 'DD-MON-YY') from dual;
its throw error ?

parameter (input) is same, so why oracle throw error


Do not tell us what you do and get (you may be wrong in what you wrote and saw), SHOW US, copy and paste your SQL*Plus session.
Before, read How to use [code] tags and make your code easier to read.

Re: Need to convert into date for differernt kind of formates [message #647701 is a reply to message #647698] Fri, 05 February 2016 10:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
suwrna wrote on Fri, 05 February 2016 17:20
select to_char('25-DEC-16') from dual;

result is 25-DEC-16 it working

but
select to_char('25-DEC-16', 'DD-MON-YY') from dual;
its throw error ?

parameter (input) is same, so why oracle throw error


Michel Cadot wrote on Fri, 05 February 2016 16:49

In the end, when else failed, read Database SQL Reference


Especially TO_CHAR function.


Re: Need to convert into date for differernt kind of formates [message #647742 is a reply to message #647698] Sat, 06 February 2016 16:04 Go to previous message
EdStevens
Messages: 1377
Registered: September 2013
Senior Member
suwrna wrote on Fri, 05 February 2016 10:20
select to_char('25-DEC-16') from dual;

result is 25-DEC-16 it working

but
select to_char('25-DEC-16', 'DD-MON-YY') from dual;
its throw error ?

parameter (input) is same, so why oracle throw error


You continue to mis-understand the difference between a DATE and a CHARACTER STRING that represents a DATE.
And you do not seem to understand the difference between the purpose of TO_DATE vs TO_CHAR.

'25-DEC-16' is not a DATE. It is string of characters the represent a date. I cannot show you a DATE here, because it is a binary value and a computer screen is, ultimately, only capable of displaying character strings.

http://docs.oracle.com/database/121/SQLRF/sql_elements001.htm#i45694
http://docs.oracle.com/database/121/SQLRF/sql_elements001.htm#sthref120
http://docs.oracle.com/database/121/SQLRF/functions216.htm#SQLRF06129
http://docs.oracle.com/database/121/SQLRF/functions219.htm#SQLRF06132

(besides the fact that you hijacked someone else's thread)
Previous Topic: Variables
Next Topic: help with a query
Goto Forum:
  


Current Time: Thu Jun 25 05:25:01 CDT 2026