Search code examples
sqlinformix

function on Informix to replace characters doesn't work


I have been try to crete a function to get a value, replace a specific character and return it.

Data on table caracteres

caracter remplazo 
Ñ          N
ñ          n
á          a
é          e
Ê          E                              

Function

create function "informix".arreglar_cadenaAndSec(rt_nombre char(50))
    Returning char(50)
    Define fix_caracter char(3);
    Define fix_remplazo char(3);
    FOREACH SELECT caracter,remplazo
        INTO fix_caracter,fix_remplazo
        FROM caracteres     
        Let rt_nombre=replace(rt_nombre,fix_caracter,fix_remplazo);
    END FOREACH
    return rt_nombre;
End Function; 

Input

FÊLIX                                        

Output

FÊLIX                                        

Desired output

FELIX                                        

Solution

  • Try using the TRIM() function on the old_string and new_string arguments to the REPLACE() function as in this example:

    create table characters(character char(3), replchar char(3));
    insert into characters values("A", "a");
    insert into characters values("C", "c");
    insert into characters values("E", "e");
    
    create function replchar(s1 char(50))
    returning char(50);
    define cin char(3);
    define crepl char(3);
    foreach select character, replchar into cin, crepl
    from characters
      let s1 = replace(s1, trim(cin), trim(crepl));
    end foreach
    return s1;
    end function;
      
    execute function replchar("ABCDEF");
    

    This gives the output:

    aBcDeF