Manipular String, Instr, Substr, Pathname, Filename e Tag

Dúvidas, dicas e truques de PL/SQL. Aqui também vão assuntos relacionados a pacotes, triggers, funções, Java-Stored Procedures, etc
Responder
Renato Menezes Viana
Rank: Analista Pleno
Rank: Analista Pleno
Mensagens: 128
Registrado em: Sáb, 18 Nov 2006 11:51 am
Localização: Rio de Janeiro - RJ

Pessoal: Deve existir um monte de rotinas parecidas, mas pensando naquelas funções de pegar o Pathname / Filename, acho estas funções de pegar uma quantidade de caracteres à partir da direita ou da esquerda de determinado TAG fica bem melhor do que lembrar dos parâmetros SUBSTR e INSTR.
Vejam aí o que acham:

Selecionar tudo

create or replace 
function Z_get_left_tag
   (p_path IN VARCHAR2, p_tag IN VARCHAR2)
   RETURN varchar2

IS
   v_dir VARCHAR2(2048);

BEGIN
/* Analyst Renato Viana http://br.linkedin.com/pub/renato-menezes-viana/35/573/11b/
  ------ !!!!!! TEST ONLY !!!!!!!!!
/* Pull the string to the left of the tag. Search starts at the left

SELECT Z_get_left_tag('c:\windows\temp\example.xls', '\temp\example.xls')
FROM dual;

c:\windows

SELECT Z_get_left_tag('c:\windows\temp\example.xls', '\')
FROM dual;

c:

*/

   -- strings not null
   IF p_path is not null
     AND p_tag is not null
      AND INSTR (p_path,p_tag,1,1) > 0
       AND length(p_tag) < length(p_path) THEN
      v_dir := SUBSTR(p_path,1,( INSTR (p_path,p_tag,1,1) - 1 ) );
      

   -- If null return the original string
   ELSE
      v_dir := p_path;
   END IF;

   RETURN v_dir;

END;

create or replace 
function Z_get_right_tag
   (p_path IN VARCHAR2, p_tag IN VARCHAR2)
   RETURN varchar2

IS
   v_file VARCHAR2(2048);

BEGIN
/* Analyst Renato Viana http://br.linkedin.com/pub/renato-menezes-viana/35/573/11b/
  ----- !!!!!!!!!!!!!! TEST ONLY !!!!!!!!!!!!!!!!
/* Pull the string to the right of the tag. Search begins in the right

SELECT Z_get_right_tag('c:\temp\example.xls', 'c:\temp\')
FROM dual;

example.xls

SELECT Z_get_right_tag('c:\temp\example.xls', '\')
FROM dual;

example.xls

*/

   -- strings no null
   IF p_path is not null
     AND p_tag is not null
      AND INSTR(p_path,p_tag,-1,1) > 0
       AND length(p_tag) < length(p_path) THEN
      v_file := SUBSTR(p_path,(INSTR(p_path,p_tag,-1,1)+length(p_tag)),length(p_path));

   -- If null, return the original string
   ELSE
      v_file := p_path;

   END IF;

   RETURN v_file;

END;
EXEMPLOS:

Selecionar tudo

SELECT Z_get_left_tag('c:\windows\temp\example.xls', '\temp\example.xls')
FROM dual;

c:\windows

Selecionar tudo

SELECT Z_get_left_tag('c:\windows\temp\example.xls', '\')
FROM dual;

c:

Selecionar tudo

SELECT Z_get_right_tag('c:\temp\example.xls', 'c:\temp\')
FROM dual;

example.xls

Selecionar tudo

SELECT Z_get_right_tag('c:\temp\example.xls', '\')
FROM dual;

example.xls
Avatar do usuário
stcoutinho
Moderador
Moderador
Mensagens: 850
Registrado em: Qua, 11 Mai 2011 5:15 pm
Localização: são Paulo - SP

Renato,

Interessante seu código.

Também existe um exemplo para recuperar informações de diretório via "EXTERNAL TABLE".

Maiores informações neste link:

http://www.oracle.com/technetwork/issue ... 12277.html

Não cheguei a estudar o exemplo, mas me parece possivel adaptar o mesmo para UNIX.

Abraços,

Sergio Coutinho
Renato Menezes Viana
Rank: Analista Pleno
Rank: Analista Pleno
Mensagens: 128
Registrado em: Sáb, 18 Nov 2006 11:51 am
Localização: Rio de Janeiro - RJ

Coutinho: embora estes exemplos mostrem recuperação de strings contendo Pathname\Filename as funções podem recuperar qualquer conteúdo dos strings.
Outros exemplos:

SELECT Z_get_right_tag('<style>com \ ou com / tanto faz</style>', '</style>')
FROM dual;

com \ ou com / tanto faz

Valeu!
Renato Menezes Viana
Rank: Analista Pleno
Rank: Analista Pleno
Mensagens: 128
Registrado em: Sáb, 18 Nov 2006 11:51 am
Localização: Rio de Janeiro - RJ

Coutinho: fiz enorme besteira no exemplo. Corrigindo...

SELECT Z_get_left_tag('<style>com \ ou com / tanto faz</style>', '</style>')
FROM dual;

<style>com \ ou com / tanto faz
Responder
  • Informação
  • Quem está online

    Usuários navegando neste fórum: Majestic-12 [Bot] e 6 visitantes