SET TERM ^ ;

CREATE OR ALTER FUNCTION FN_MES_ANIVERSARIO_CLI (DATA_ANIVERSARIO VARCHAR(20))
RETURNS INTEGER
AS
declare variable MES_ANIVERSARIO INTEGER;
declare variable PARTES VARCHAR(1000);
BEGIN
    
    :MES_ANIVERSARIO = 0;

    IF(:DATA_ANIVERSARIO IS NOT NULL) THEN
    BEGIN
        :DATA_ANIVERSARIO = TRIM(:DATA_ANIVERSARIO);
        :DATA_ANIVERSARIO = REPLACE(:DATA_ANIVERSARIO, '-', '.');
        :DATA_ANIVERSARIO = REPLACE(:DATA_ANIVERSARIO, '/', '.');

        IF(CHAR_LENGTH(:DATA_ANIVERSARIO) = 10) THEN
        BEGIN
            :PARTES = FN_ARRAY(:DATA_ANIVERSARIO, 2, '.');


            :MES_ANIVERSARIO = CAST(:PARTES AS INTEGER);
    
        END

        IF(CHAR_LENGTH(:DATA_ANIVERSARIO) = 5) THEN
        BEGIN
            :PARTES = FN_ARRAY(:DATA_ANIVERSARIO, 2, '.');

            :MES_ANIVERSARIO = CAST(:PARTES AS INTEGER);
    
        END
    END
    

    RETURN :MES_ANIVERSARIO;
END
^

SET TERM ; ^

