Com a ajuda do colegas cheguei a uma função que vai me ajudar, segue aí pra
quem precisar:

Primeiro criamos a função de insert, chamei de finsert pra não confundir
com insert do SQL :)

CREATE OR REPLACE FUNCTION finsert(text, text, integer)
  RETURNS text AS
$BODY$
  select overlay($2 placing $1 from $3 for 0);
$BODY$
  LANGUAGE sql IMMUTABLE
  COST 100;
ALTER FUNCTION finsert(text, text, integer)
  OWNER TO postgres;


Então criamos a função para formatar os telefones conforme o numero de
caractere, que deve ter no minimo 10 digitos

CREATE OR REPLACE FUNCTION fgetmascfone(text)
  RETURNS character varying AS
$BODY$
  declare
    fone text;
begin
  fone = $1;
  if (length(fone) < 10) then
    fone = right('          '||fone, 10); -- caso não haja DDD colocamos 10
caracteres no minimo
  end if;
  if (left(fone, 4) = '0800') then
    case
    when length(fone) in(10,11) then
      fone = finsert(' ', fone, 5);
      fone = finsert('-', fone, 9);
    when length(fone) in(12) then
      fone = finsert(' ', fone, 5);
      fone = finsert('-', fone, 10);
    when length(fone) in(13,14) then
      fone = finsert(' ', fone, 5);
      fone = finsert('-', fone, 10);
      fone = finsert('-', fone, 15);
    else fone = fone;
    end case;
  else
    case
    when length(fone) in(9) then
      fone = finsert('(', fone, 1);
      fone = finsert(') ', fone, 4);
      fone = finsert('-', fone, 9);
    when length(fone) in(10) then
      fone = finsert('(', fone, 1);
      fone = finsert(') ', fone, 4);
      fone = finsert('-', fone, 10);
    when length(fone) in(11) then
      fone = finsert('(', fone, 1);
      fone = finsert(') ', fone, 4);
      fone = finsert('-', fone, 7);
      fone = finsert('-', fone, 12);
    when length(fone) in(12,13,14) then
      fone = finsert('(', fone, 1);
      fone = finsert(') ', fone, 4);
      fone = finsert('-', fone, 7);
      fone = finsert('-', fone, 12);
      fone = finsert('-', fone, 17);
    else fone = fone;
    end case;
  end if;
  return fone;
end;
$BODY$
  LANGUAGE plpgsql IMMUTABLE
  COST 100;
ALTER FUNCTION fgetmascfone(text)
  OWNER TO postgres;


A função foi feito meio na correria, mas pelos testes está a contento :)
depois (bem depois rsrsrs) dou mais uma garimpada.


Fica aí pra quem precisar

Obrigado a todos mais uma vez...


Marcelo Silva
-------------------------------------------



Em 3 de setembro de 2013 12:37, Matheus de Oliveira <
[email protected]> escreveu:

>
>
>
> 2013/9/3 Juliano Atanazio <[email protected]>
>
>>
>>
>>
>> Em 3 de setembro de 2013 12:23, Matheus de Oliveira <
>> [email protected]> escreveu:
>>
>>>
>>>
>>>
>>> 2013/9/3 Marcelo da Silva <[email protected]>
>>>
>>>> Pessoal no Delphi tenho a função INSERT que serve para inserir uma
>>>> string dentro da outra, por exemplo:
>>>>
>>>> S := insert('s', 'tete', 3);
>>>>
>>>> S = teste
>>>>
>>>> Existe alguma função que faça isso no postgres?
>>>> Já procurei no manuel, mas não encontrei.
>>>>
>>>>
>>> Existe a função overlay [1]:
>>>
>>> SELECT overlay('tete' placing 's' from 3 for 0);
>>>
>>> Você pode criar uma insert +ou- assim:
>>>
>>> CREATE FUNCTION insert(text,text,int)
>>> RETURNS TEXT
>>> LANGUAGE SQL AS $$
>>>     SELECT overlay($1 placing $2 from $3 for 0);
>>> $$;
>>>
>>> [1]
>>> http://www.postgresql.org/docs/current/static/functions-binarystring.html
>>>
>>>
>>>
>> O exemplo do Matheus foi muito mais objetivo rs
>> De qualquer forma é bom ter uma certa variedade de formas de fazer a
>> mesma coisa.
>>
>
> =D
>
>
>> Adicionalmente, este link [1] tem funções de tratamento de strings no
>> PostgreSQL.
>> Extremamente útil :)
>>
>> [1] http://www.postgresql.org/docs/9.2/static/functions-string.html
>>
>>
>>
> Faz mais sentido que o link que eu mandei... aliás, o link que eu mandei
> não faz sentido algum (é pra bytea, apesar de ter a mesma função),
> considerem este do Juliano, xD...
>
> Atenciosamente,
> --
> Matheus de Oliveira
> Analista de Banco de Dados
> Dextra Sistemas - MPS.Br nível F!
> www.dextra.com.br/postgres
>
>
> _______________________________________________
> pgbr-geral mailing list
> [email protected]
> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>
>


-- 
Marcelo Silva
----------------------------------------------------------------
Desenvolvedor Delphi / PHP
My Postgres database
Cel.: (11) 99693-4251
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a