check if “it's a number” function in Oracle

后端 未结 16 2774
一整个雨季
一整个雨季 2020-11-28 10:31

I\'m trying to check if a value from a column in an oracle (10g) query is a number in order to compare it. Something like:

select case when ( is_number(myTab         


        
16条回答
  •  死守一世寂寞
    2020-11-28 11:10

    I'm against using when others so I would use (returning an "boolean integer" due to SQL not suppporting booleans)

    create or replace function is_number(param in varchar2) return integer
     is
       ret number;
     begin
        ret := to_number(param);
        return 1; --true
     exception
        when invalid_number then return 0;
     end;
    

    In the SQL call you would use something like

    select case when ( is_number(myTable.id)=1 and (myTable.id >'0') ) 
                then 'Is a number greater than 0' 
                else 'it is not a number or is not greater than 0' 
           end as valuetype  
      from table myTable
    

提交回复
热议问题