Pular para o conteúdo

Fóruns SQL e PL/SQL comparando string comparando string

#96243
fsitja
Participante

    Não sei se entendi direito. São duas tabelas diferentes, uma com a coluna contendo o valor ‘2010_07_012542’ e na outra tabela há uma coluna que possui valores tipo ‘hsbc fundos – 012542’, e você quer fazer um join nas duas, sem possuir um relacionamento por FK?

    Se for isso tente o seguinte:


    SQL> create table t1 as
    2 select '2010_07_012542' txt1 from dual;

    Table created
    SQL> create table t2 as
    2 select 'hsbc fundos - 012542' txt2 from dual;

    Table created

    SQL>
    SQL> select t1.txt1, t2.txt2, regexp_substr(t1.txt1, 'd+$') numero
    2 from t1
    3 join t2 on regexp_substr(t1.txt1, 'd+$') = regexp_substr(t2.txt2, 'd+$');

    TXT1 TXT2 NUMERO


    2010_07_012542 hsbc fundos - 012542 012542

    SQL>

    Se a consulta ficar lenta, você pode criar um índice (function-based index) nas tabelas, para não precisar executar a função durante o processamento do SQL:

    create index ix_fn_regexp_txt1 on t1 (regexp_substr(txt1, 'd+$'));
    create index ix_fn_regexp_txt2 on t2 (regexp_substr(txt2, 'd+$'));