How to select "1" or "2" depending on the column value in PL / SQL?

4

I have a column in my TIPOCLIENT table. In it appear CPF for Individual and CNPJ for Legal Entity.

I would like the moment SELECT to come "1" to CPF and "2" to CNPJ. How can I do this?

    
asked by anonymous 05.03.2014 / 12:40

4 answers

5

Use the CASE conditional.

SELECT lista_de_campos,   
    CASE tipocliente  
        WHEN 'CPF' THEN 1  
        ELSE 2  
    END AS novo_tipo_cliente  
FROM sua_tabela;
    
05.03.2014 / 13:08
0
select c.cpf_cnpj,
       case
          when length(c.cpf_cnpj) > 11 then
           2
          else
           1
       end case
  from (
        select '12345655551' cpf_cnpj
          from dual
        union
        select '01234567000115' cpf_cnpj
          from dual) c
    
04.09.2014 / 00:06
0
Select decode(tipo,1,cpf,2,cnpj,null) campo
From tabela

A solution with DECODE. It helped!?

    
04.09.2014 / 02:05
0

It would be this:

Select decode(TIPOCLIENTE,'CPF',1,'CNPJ',2,0) TIPOCLIENTE
  From tabela

In this case, the value '0' will return if the value is different from the CPF or CNPJ. Also it would give to treat for if it does not find bring the own column, but there it has to verify the type of data, therefore as it is a string must return string.

    
10.03.2015 / 19:34