I have a sql script file that dynamically generates a control file. It accepts date fields in date formats for mm/dd/yyyy. The sqlldr is loading the dates from the csv file, but it is also accepting date formats such as "mm\dd\yyyy" or "mm.dd.yyyy". How do i make it only accept MM/DD/YYYY?
set echo off ver off feed off pages 0
accept fname prompt 'Enter Name of File: '
spool &fname..ctl
select 'OPTIONS (SKIP=1)' || chr (10) ||
'LOAD DATA'|| chr (10) ||
'DISCARDMAX 99999' || chr (10) ||
'APPEND'||chr (10)||
'INTO TABLE MY_TABLE' || chr (10)||
'FIELDS TERMINATED BY '',''' || chr (10)||
'OPTIONALLY ENCLOSED BY ''"''' || chr (10) ||
'TRAILING NULLCOLS' || chr(10) ||
' (col1,' || chr (10) ||
' col2,' || chr (10) ||
' col3,' || chr (10) ||
' col4,' || chr (10) ||
' col5,' || chr (10) ||
' col6,' || chr (10) ||
' col7 DATE "mm/dd/yyyy",' || chr (10) ||
' col8 DATE "mm/dd/yyyy",' || chr (10) ||
' col9,' || chr (10) ||
' col10,' || chr (10) ||
' col11,' || chr (10) ||
' col12,' || chr (10) ||
' col13,' || chr (10) ||
' col14,' || chr (10) ||
' col15,' || chr (10) ||
' col16,' || chr (10) ||
' processid "' || MAX(processid) || '",' || chr (10) ||
' staging_id "1",' || chr (10) ||
' FILENAME "''&fname''",' || chr (10) ||
' LINENO SEQUENCE(1)' || chr (10) ||
' )' || chr (10)
from process_id
where filename = '&fname';
Why worry about the separator? Consider this procedure that is a member of our utility package that I threw together for a similar need. You pass it a table name and a separator and it reads USER_TAB_COLUMNS and outputs a skeleton control file which I then save into a file (I use Toad but of course you could spool it into a file too). I use it all the time. It's not pretty, but it meets my needs. Tweak to meet your needs, I believe it may save you some time retyping column names and data types.
Source:
Run it:
Output:
I found the answer. Date formatting in oracle allows options FX and FM for exact formatting
for example
returns 6/21/2016
will return error "literal does not match format string"
so in my control file sql script i added the FX and FM commands
now only dates with exactly mm/dd/yyyy format will be accepted and the rest will be rejected as a bad row