How to concatenate a varibale int and str in a trigger?


I'm doing a trigger in sql which concatenates a word with a number, so this is:

create or replace trigger TG_curso_Auto
before insert on cursos
for each row
declare  var int;
    select count(*)+1  into var from cursos;
    :new.siglas := concat(:new.siglas,'-',var);

Here var is an integer variable that I need to concatenate with a string, but I do not know how to pass it to string, the courses table simply has an id and an acronym

create table cursos
     id number(7) not null,
     siglas varchar(4) not null);

To better explain the var start from 1 and as you add something goes adding 1 to 1 ... when trying to insert into the table courses for example ( insert into cursos values (12345,'Bases') ) should put in the column acronyms Bases-1 then something like acronyms-2 , < em> acronyms-3 ...

Any ideas on how to solve this problem or is it not possible to do this?

Thank you!

asked by DDR 10.11.2018 в 04:26

2 answers


use the to_char function, for example as follows :

:new.siglas := :new.siglas || '-' || to_char(var);

In this case, the field will remain in null if it already had a null value. If it is not the desired behavior, you can also use coalesce , for example

:new.siglas := coalesce(:new.siglas, '') || '-' || to_char(var);
answered by 10.11.2018 / 21:52

to concatenate you can use the double bars: campo1 || '-' || campo2

answered by 10.11.2018 в 09:38