jueves, 27 de junio de 2013
Get Parent Ledger Account
/*
"1-1-1-00-0-00"
"1-1-1-01-0-00"
"1-1-1-01-1-00"
"1-1-1-01-2-00"
"1-1-1-02-0-00"
"1-1-1-02-1-00"
"1-1-1-02-1-01"
"1-1-1-02-1-02"
"1-1-1-02-1-03"
select sp_obtener_cta_padre('11102103');
select sp_obtener_cta_padre('11102100');
select sp_obtener_cta_padre('11102000');
*/
create or replace function sp_obtener_cta_padre(p_cuenta varchar)
returns varchar as $$
declare
nivel1 integer;
nivel2 integer;
nivel3 integer;
nivel4 integer;
nivel5 integer;
nivel6 integer;
n1 varchar;
n2 varchar;
n3 varchar;
n4 varchar;
n5 varchar;
n6 varchar;
b boolean;
resultado varchar;
begin
nivel1 = cast(substring(p_cuenta, 1, 1) as integer);
nivel2 = cast(substring(p_cuenta, 2, 1) as integer);
nivel3 = cast(substring(p_cuenta, 3, 1) as integer);
nivel4 = cast(substring(p_cuenta, 4, 2) as integer);
nivel5 = cast(substring(p_cuenta, 6, 1) as integer);
nivel6 = cast(substring(p_cuenta, 7, 2) as integer);
b = true;
if (nivel6 > 0 and b = true) then
nivel6 = 0;
b = false;
end if;
if (nivel5 > 0 and b = true) then
nivel5 = 0;
b = false;
end if;
if (nivel4 > 0 and b = true) then
nivel4 = 0;
b = false;
end if;
if (nivel3 > 0 and b = true) then
nivel3 = 0;
b = false;
end if;
if (nivel2 > 0 and b = true) then
nivel2 = 0;
b = false;
end if;
n1 = cast(nivel1 as varchar);
n2 = cast(nivel2 as varchar);
n3 = cast(nivel3 as varchar);
n4 = cast(nivel4 as varchar);
n5 = cast(nivel5 as varchar);
n6 = cast(nivel6 as varchar);
if(char_length(n4) = 1)then
n4 = '0' || n4;
end if;
if(char_length(n6) = 1)then
n6 = '0' || n6;
end if;
resultado = n1 || n2 || n3 || n4 || n5 || n6;
return resultado;
end;
$$ language plpgsql;
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario