You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
createfunctionformatCnpjCpf(value varchar2) return varchar2
is
cnpj_cpf varchar2(14) := regexp_replace(value, '(\D)', '');
begin
if length(cnpj_cpf) =11 then
return regexp_replace(cnpj_cpf, '(\d{3})(\d{3})(\d{3})(\d{2})','\1.\2.\3-\4');
end if;
return regexp_replace(lpad(cnpj_cpf, 14, '0'),'(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})','\1.\2.\3/\4-\5');
end;
PostgreSQL
CREATE OR REPLACE FUNCTION public.formatCpfCnpj(cpfcnpj character varying)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
DECLARE
cnpjcpf_only_numbers varchar;
BEGIN
cnpjcpf_only_numbers = regexp_replace(cpfcnpj, '(\D)', ''); -- Remove todos os caracteres, exceto números
case length(cnpjcpf_only_numbers)
when 11 then
return regexp_replace(cnpjcpf_only_numbers, '(\d{3})(\d{3})(\d{3})(\d{2})','\1.\2.\3-\4');
when 14 then
return regexp_replace(cnpjcpf_only_numbers, '(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})','\1.\2.\3/\4-\5');
else
return cnpjcpf_only_numbers;
END case;
END;
$function$
;