0

I would like to migrate this postgre function to mysql. The function receives two dates as a parameter and returns an array with the intermediate dates received.

example:

send two dates '2019-01-01' & '2019-05-31'.

i get: {'2019-01-01','2019-02-01','2019-03-01','2019-04-01','2019-05-01'}

The function postgre:

    CREATE OR REPLACE FUNCTION schema.date_range(
    start_date date,
    end_date date)
RETURNS date[]
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE 
--    ROWS 0
AS $BODY$

DECLARE
date_exe date := start_date;
array_date date[];
BEGIN

WHILE date_exe <= end_date
LOOP
    -- MAKE YOUR STUFF
    RAISE NOTICE '%', date_exe;
    array_date := array_append(array_date,date_exe);
    date_exe := date_exe + interval '1 month';
END LOOP;

RETURN array_date;

END;

$BODY$;

The idea is to insert in a table the periods per month between two dates, with the range returned (from the postgre function) performed a for loop to insert these values:

SET array_date_range = schema.date_range( '2019-01-01', '2019-05-31'); -- function I want to migrate

for i in  coalesce(array_lower(array_date_range, 1),1)..coalesce(array_upper((array_date_range, , 1),1) loop


 SELECT LAST_DAY((array_date_range[i]) INTO v_date_end;


 INSERT INTO schama.table (date_ini, date_end)

 VALUES (array_date_range[i] ,v_date_end);

end loop;

thanks in advance

In0cybe
  • 301
  • 1
  • 2
  • 11
  • 1
    Then see the answer to the following SO question: https://stackoverflow.com/questions/2157282/generate-days-from-date-range/ Just use the select as part of an insert ... select ... and that's it. – Shadow Oct 28 '19 at 16:12
  • perfect! Very thankful Shadow. – In0cybe Oct 28 '19 at 16:32

0 Answers0