SET TERM ^ ;

CREATE OR ALTER function FN_START_OF_WEEK (
    I_DATA varchar(100) = null)
returns timestamp
AS
declare variable SM integer;
declare variable data timestamp;
begin
    DATA = CAST(I_DATA AS TIMESTAMP);

    IF ( DATA IS NULL ) THEN
    BEGIN
        DATA = CURRENT_TIMESTAMP;
    END

    SM  = EXTRACT(weekday FROM DATA);

    return dateadd(day, SM * -1, DATA);
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER function FN_END_OF_WEEK (
    I_DATA varchar(100) = null)
returns timestamp
AS
declare variable SM integer;
declare variable data timestamp;
begin
    DATA = CAST(I_DATA AS TIMESTAMP);

    IF ( DATA IS NULL ) THEN
    BEGIN
        DATA = CURRENT_TIMESTAMP;
    END

    SM  = EXTRACT(weekday FROM DATA);

    return dateadd(day, 6 - SM, DATA);
end
^

SET TERM ; ^


