create function sys.truncate1(numeric,int4)
returns numeric
as $$
declare
left_value numeric;
right_value numeric;
temp varchar;
temp1 varchar;
res numeric;
begin
left_value := length(substring(cast($1 as varchar), position('.' in cast($1 as varchar))+ 1));
right_value := length($1 ::varchar) - left_value -1;
if $2 >= 0 then
if left_value = right_value then
return 0;
else
temp := substr(cast($1 as varchar), 1, right_value-ABS($2));
temp1 := RPAD(temp, ABS($2) + length(temp), '0');
return TO_NUMBER(temp1,'99999999999999999999.999999999999999999999999');
end if;
end if;
end;
$$ language plpgsql;