| Char to Date Conversion [message #576676] |
Thu, 07 February 2013 03:25  |
 |
yuvraaj
Messages: 94 Registered: January 2011 Location: Bangalore, INDIA
|
Member |
|
|
Hi,
Need quick help in converting a string to date, my string looks like '01022013' '04022013', I tried to_date('01022013', 'dd-mm-yyyy') but couldn't work.
Thanks in advance
YJ
|
|
|
|
|
|
| Re: Char to Date Conversion [message #576691 is a reply to message #576677] |
Thu, 07 February 2013 05:50   |
cookiemonster
Messages: 9141 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Actually it's not obviously wrong, oracle doesn't care it you skip the dashes:
SQL> SELECT to_date('01022013', 'dd-mm-yyyy') FROM dual
2 /
TO_DATE('010220
---------------
20130201 000000
So yuvraaj - in what way is it not working?
|
|
|
|
|
|
| Re: Char to Date Conversion [message #576709 is a reply to message #576701] |
Thu, 07 February 2013 07:36   |
 |
Michel Cadot
Messages: 54155 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Yes, Oracle does not care about constant characters but only when constants are only 1 character unless you omit the whole constant part:
SQL> select to_date('01#02|2013', 'dd-mm-yyyy') from dual;
TO_DATE('01#02|2013
-------------------
01/02/2013 00:00:00
SQL> select to_date('01#022013', 'dd-mm-yyyy') from dual;
TO_DATE('01#022013'
-------------------
01/02/2013 00:00:00
SQL> select to_date('01022013','DD------MM-----YYYY') from dual;
TO_DATE('01022013',
-------------------
01/02/2013 00:00:00
SQL> select to_date('01Michel02Cadot2013','DD------MM-----YYYY') from dual;
select to_date('01Michel02Cadot2013','DD------MM-----YYYY') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
But if the constant string is between " then it is a real constraint:
SQL> select to_date('01022013','dd"-"mm"-"yyyy') from dual;
select to_date('01022013','dd"-"mm"-"yyyy') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> select to_date('01#02/2013', 'dd"-"mm"-"yyyy') from dual;
select to_date('01#022013', 'dd"-"mm"-"yyyy') from dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL> select to_date('01-02-2013', 'dd"-"mm"-"yyyy') from dual;
TO_DATE('01-02-2013
-------------------
01/02/2013 00:00:00
Regards
Michel
|
|
|
|
|
|
| Re: Char to Date Conversion [message #576857 is a reply to message #576676] |
Fri, 08 February 2013 10:34  |
joy_division
Messages: 4265 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
yuvraaj wrote on Thu, 07 February 2013 04:25Hi,
Need quick help in converting a string to date, my string looks like '01022013' '04022013', I tried to_date('01022013', 'dd-mm-yyyy') but couldn't work.
Thanks in advance
YJ
It works for me. Based on the answers and your final posting, you cannot possibly be telling us the whole truth, since
SQL> select to_date('01022013', 'dd-mm-yyyy') from dual;
TO_DATE('01022013',
-------------------
02/01/2013 00:00:00
works.
|
|
|
|