Home » SQL & PL/SQL » SQL & PL/SQL » TO_DATE formating (Oracle 11.2.0.3)
TO_DATE formating [message #650189] Tue, 19 April 2016 03:27 Go to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Hi,

Could you please help me in understanding why oracle treats the first set and second set differently.
In the first set oracle is able to match the format but not in second set.

I am aware of the fact that format matching should be done either by FX or "" as Solomon posted here.
https://community.oracle.com/thread/2412493?tstart=0

Set 1:

select to_date('20120704','YYYYMMDD') from dual ;

TO_DATE('20
-----------
04-JUL-2012

1 row selected.

SQL> select to_date('20120704','YYYY/MM/DD') from dual ;

TO_DATE('20
-----------
04-JUL-2012

1 row selected.

SQL> select to_date('20120704','YYYY-MM-DD') from dual ;

TO_DATE('20
-----------
04-JUL-2012

1 row selected.

SQL> select to_date('20120704','YYYY-----MM-----DD') from dual ;

TO_DATE('20
-----------
04-JUL-2012

1 row selected.

Set 2:
select to_date('20120704','YYYY/MM/DD') from dual ;


Is it because as Tom said -
Quote:
because - and / are similar to "whitespace might be here" without the format exact specification.

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:254129800346358589

Thank you in advance.

Regards,
Pointers


Re: TO_DATE formating [message #650190 is a reply to message #650189] Tue, 19 April 2016 03:43 Go to previous messageGo to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Sorry
Set 2 is
select  to_date('07/09/2014','mmddyyyy') from  dual


Regards,
Pointers
Re: TO_DATE formating [message #650191 is a reply to message #650190] Tue, 19 April 2016 03:55 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The documentation on format masks states that oracle gives flexibility if the string to be converted doesn't contain all the punctuation characters (-/,.;: <space>) in the format mask. You can skip some or all or use a different punctuation character and as long as all the numerics are present it'll still work.
It gives you no such flexibility the other way around. So if the string has more punctuation than the mask you'll get an error.
That's just what oracle decided.
Re: TO_DATE formating [message #650193 is a reply to message #650191] Tue, 19 April 2016 05:51 Go to previous messageGo to next message
pointers
Messages: 451
Registered: May 2008
Senior Member
Thank you.

Regards,
Pointers
Re: TO_DATE formating [message #650204 is a reply to message #650193] Tue, 19 April 2016 08:28 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
And of course on all of these you are simply selecting a to_date() value. That means that to DISPLAY the RESULT, oracle has to do an implicit to_char() on the result of your to_date(). So that to_char will always present the date in the format specified by the controlling setting of NLS_DATE_FORMAT.

See "Understanding Date Formats" at http://edstevensdba.com/blog-article-index/
Re: TO_DATE formating [message #650205 is a reply to message #650204] Tue, 19 April 2016 08:34 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
If you're using sqlplus nls_date_format applies, if you're using a GUI then other settings usually take precedence.
Previous Topic: query to right outer join and sum up
Next Topic: need to modify the column datatype from varchar to number
Goto Forum:
  


Current Time: Thu Apr 25 19:59:40 CDT 2024