Search code examples
oracletypesoracle-sqldeveloper

How to correctly define an array of date type in Oracle?


I want to convert a string array to a date type array. But I'm not sure if my date array is correctly defined. Can't find any example of how to define or create an array of date type.

This is how I have declared my string array and date type array.

create or replace TYPE array_collection IS table OF VARCHAR2(50) 
--string array is working absolutely fine so dw about that

create or replace type date_array is table of date;
--here i don't if I've defined this array correctly

my convert array procedure:

create or replace procedure convert_arr(
dat_ar in array_collection,perdate_arr out date_array)
as
begin
perdate_arr:=new date_array();
perdate_arr.extend(dat_ar.count);
for i in 1..dat_ar.count loop
perdate_arr(i):=to_date(dat_ar(i), 'yyyy-mm-dd');
end loop;
end convert_arr;
--compiles perfectly

calling anonymous block:

set serveroutput on
declare 
--l_dat array_collection;
--l_darray date_array;
l_dat array_collection:=array_collection();
l_darray date_array:=date_array();
begin
l_dat := array_collection('2011-01-01','2011-04-01','2011-05-01');
--l_dat.extend(3);
-- l_dat(1):= to_date(2019-07-08);
-- l_dat(2):= to_date(2019-07-09);
-- l_dat(3):= to_date(2019-06-02);

convert_arr(dat_ar=>l_dat,perdate_arr=>l_darray);
dbms_output.put_line('Number of array:' || l_dat.count);
for i in 1..l_dat.count loop
dbms_output.put_line('Date ' || i || ':' || to_char(l_dat(i),'dd/mm/yyyy'));
end loop;
end;

this block gives me an error:

Error report -
ORA-01861: literal does not match format string
ORA-06512: at line 9
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

Tried changing formats but doesn't help. Any help would be highly appreciated. Thankyou


Solution

  • Since l_darray is your date array, loop through it for displaying rather than l_dat

    set serveroutput on
    declare 
    --l_dat array_collection;
    --l_darray date_array;
    l_dat array_collection:=array_collection();
    l_darray date_array:=date_array();
    begin
    l_dat := array_collection('2011-01-01','2011-04-01','2011-05-01');
    --l_dat.extend(3);
    -- l_dat(1):= to_date(2019-07-08);
    -- l_dat(2):= to_date(2019-07-09);
    -- l_dat(3):= to_date(2019-06-02);
    
    convert_arr(dat_ar=>l_dat,perdate_arr=>l_darray);
    dbms_output.put_line('Number of array:' || l_darray.count);
    for i in 1..l_darray.count loop
    dbms_output.put_line('Date ' || i || ':' || to_char(l_darray(i),'dd/mm/yyyy'));
    end loop;
    end;
    /
    

    Result

    Number of array:3
    Date 1:01/01/2011
    Date 2:01/04/2011
    Date 3:01/05/2011
    
    
    PL/SQL procedure successfully completed.