create or replace package body dt is
/*
|| Declare the structure of the PL/SQL table which will hold
|| the masks. Then declare the table itself.
*/
type mask_tabtype is
table of varchar2(30) index by binary_integer;
fmts mask_tabtype;
function val (
value_in in varchar2
) return date is
retval date := null;
/* Loop index for the scan through the masks */
mask_index integer := fmts.FIRST;
/* Boolean to terminate loop if date was converted */
date_converted boolean := FALSE;
begin
-- Convert from masks in table
if value_in is null then
date_converted := TRUE;
else
/* Loop through the rows in the table... */
while
mask_index is not null
and not date_converted
loop
begin
/* Try to convert string using mask in table row */
retval := TO_DATE(
value_in,
fmts(mask_index)
);
date_converted := TRUE;
exception
when others then
retval := null;
mask_index := fmts.NEXT(mask_index);
end;
end loop;
end if;
if date_converted then
return retval;
else
raise VALUE_ERROR;
end if;
end val;
procedure showfmts is
v_row pls_integer := fmts.FIRST;
begin
p.l('Support formats for String->Date Conversion');
loop
exit when v_row is null;
p.l(fmts(v_row));
v_row := fmts.NEXT(v_row);
end loop;
end;
begin
/* ------------ Initialization Section of Package ------------*/
fmts(1) := 'DD-MON-RR';
fmts(2) := 'DD-MON-YYYY';
fmts(3) := 'DD-MON';
fmts(4) := 'MM/DD';
fmts(5) := 'MM/RR';
fmts(6) := 'MMDDRR';
fmts(7) := 'MM/YYYY';
fmts(8) := 'MM/DD/RR';
fmts(9) := 'MM/DD/YYYY';
fmts(10) := 'MMDDYYYY';
fmts(11) := 'YYYYMMDD';
fmts(12) := 'RRMMDD';
end dt;