|
|
|
|
|
|
|
Re: Convert date from MMDDCCYY to YYYYMMDD [message #487496 is a reply to message #487485] |
Tue, 28 December 2010 04:26   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
You use substr to get rid of the CC digits. Then do the conversion like Michel showed you.
Also
groovekarki wrote on Tue, 28 December 2010 09:47The following
SQL> select to_char(to_date('01051152','MMDDYYYY'),'YYYYMMDD') from dual;
when run doesn't provide any output.......
When you run that in sqlplus it definitely will give output. So what exactly are you doing to run it?
|
|
|
|
|
|
|
|
Re: Convert date from MMDDCCYY to YYYYMMDD [message #487505 is a reply to message #487480] |
Tue, 28 December 2010 04:59   |
 |
Barbara Boehmer
Messages: 9105 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
groovekarki wrote on Tue, 28 December 2010 01:34
I have imported data from a fixed length format file into an external table in oracle which has several date fields in the format MMDDCCYY. I want to convert this into YYYYMMDD and insert it into another table in oracle with date field.
Dates are not stored in any particular format in Oracle. Dates are formatted using to_char when you display them. So, if your tables truly contain date columns, not character columns, then there is no conversion to be done, just select from your external table and insert into the other table. You can also change the default format by setting the nls_date_foramt. I have provided a demo below, using your data. Some of the data is rejected because it does not match your input format. For example, 58 is not a valid day. I suspect that your input format may not be what you think it is.
SCOTT@orcl_11gR2> create or replace directory my_dir as 'c:\my_oracle_files'
2 /
Directory created.
SCOTT@orcl_11gR2> create table external_table
2 (dob date)
3 ORGANIZATION external
4 (TYPE oracle_loader
5 DEFAULT DIRECTORY MY_DIR
6 ACCESS PARAMETERS
7 (RECORDS DELIMITED BY NEWLINE
8 FIELDS TERMINATED BY WHITESPACE
9 ("DOB" CHAR(8) DATE_FORMAT DATE MASK "mmddyyyy"))
10 location ('test.dat'))
11 REJECT LIMIT UNLIMITED
12 /
Table created.
SCOTT@orcl_11gR2> select * from external_table
2 /
DOB
-----------
05-Jan-1152
15-Aug-1160
03-Jan-1151
13-May-1151
11-Jan-1126
30-Jun-1162
6 rows selected.
SCOTT@orcl_11gR2> create table db_table
2 (dob date)
3 /
Table created.
SCOTT@orcl_11gR2> insert into db_table select * from external_table
2 /
6 rows created.
SCOTT@orcl_11gR2> select * from db_table
2 /
DOB
-----------
05-Jan-1152
15-Aug-1160
03-Jan-1151
13-May-1151
11-Jan-1126
30-Jun-1162
6 rows selected.
SCOTT@orcl_11gR2> select to_char (dob, 'yyyymmdd') from db_table
2 /
TO_CHAR(
--------
11520105
11600815
11510103
11510513
11260111
11620630
6 rows selected.
SCOTT@orcl_11gR2> alter session set nls_date_format = 'yyyymmdd'
2 /
Session altered.
SCOTT@orcl_11gR2> select * from db_table
2 /
DOB
--------
11520105
11600815
11510103
11510513
11260111
11620630
6 rows selected.
SCOTT@orcl_11gR2>
|
|
|
|
|
Re: Convert date from MMDDCCYY to YYYYMMDD [message #487532 is a reply to message #487504] |
Tue, 28 December 2010 08:14  |
 |
Michel Cadot
Messages: 68757 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
groovekarki wrote on Tue, 28 December 2010 11:54using sql plus when i use this command
select to_char(to_date('11511151','MMDDYYYY'),'YYYYMMDD') from dual;
I get the following error
select to_char(to_date('11511151
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
An invalid is an invalid date. Which date is 51/11/1151? I don't see it in the calendar.
Regards
Michel
|
|
|