代码之家  ›  专栏  ›  技术社区  ›  civesuas_sine

将数字与varchar2进行比较

  •  0
  • civesuas_sine  · 技术社区  · 7 年前

    我有这个函数,我需要比较数字和varchar。

          CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
    begin
    declare odd integer;
    declare i_perecentage=0;
    begin
    
    if i_odd ='SP'
    then
    return (0);
    end if;
    
    odd:=round(to_number((1-i_perecentage/100)*i_odd),2);
      if odd<1 
      then          
     return(i_odd);
     else
     return(round(odd,2));
     end if;
    
    end;
    end;
    /
    

    注:我编辑了函数,我解决了比较的问题,现在我有另一种情况,我不喜欢… 此函数返回计算的奇数百分比。问题是,如果我在结果中的i/u百分比中通过0,则得到的结果没有小数点(例如:i/u odd=3.10,i/u percentage=0,则得到的结果是odd=3,但如果我通过i/u odd=3.10,i/u percentage=1,则得到的结果是odd=3.10)。 为什么在i_percentage=0上我得不到小数点?是吗?

    3 回复  |  直到 7 年前
        1
  •  3
  •   kfinity    7 年前

    如果你想的话 验证 在pl/sql中将varchar2字段作为数字,通常您只需尝试将其转换为数字并捕获异常。

    CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
        odd number;
    BEGIN
        -- if i_odd = 'SP' (or another non-number), this will throw an ORA-01722 
        -- exception which will be caught in the exception block, below
        odd := to_number(i_odd); -- you might want a format mask here
    
        --... now you can use "odd" as a number
    
    EXCEPTION WHEN INVALID_NUMBER THEN
        return 0;
    END;
    /
    

    您还可以在代码中间嵌套一个begin..end块,以便捕获异常,如果这样更适合您:

    CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return number as
        odd number;
    begin
    
        begin
          odd := to_number(i_odd); -- you might want a format mask here
        exception when INVALID_NUMBER then
          odd := 0;
        end;
    
        --... now you can use "odd" as a number
    
    end;
    /
    
        2
  •  3
  •   tbone    7 年前

    无法捕获无效数字异常的原因是您将输入参数声明为数字。当您调用函数时,oracle首先尝试将字符串转换为数字(当然,它失败了, 在输入你的代码之前 )中。

    如果将输入参数更改为varchar2,那么将在函数内部完成到数字的转换(在本例中是隐式的),并且可以根据需要捕获和处理无效的数字(这里我只是返回一个不同的字符串来表示问题):

    create or replace function is_odd_even(i_num in varchar2)
    return varchar2
    is
    begin
      -- conversion to number is done here
      if (mod(i_num, 2) = 0) then
        return 'EVEN';
      else
        return 'ODD';
      end if;
    
    exception
      when INVALID_NUMBER or VALUE_ERROR then
        -- do something meaningful
        return 'INV';
    end;
    

    用法示例:

    with x as (
      select '1' as val from dual
      union all
      select 'SP' as val from dual
      union all
      select '2' as val from dual
    )
    select x.val, is_odd_even(x.val)
    from x;
    

    输出:

    1   ODD
    SP  INV
    2   EVEN
    
        3
  •  0
  •   civesuas_sine    7 年前

    解决方案:

    CREATE OR REPLACE FUNCTION getOdds(i_odd in varchar2, i_id in number) return varchar2 as
    
    odd varchar2(10);
    ret_value number(4); 
    
    begin
    
    if (i_odd ='SP') or i_odd is null then 
    return 'SP';
    else
      odd :=ROUND( TO_NUMBER( ( 1 - (play_beting.get_odds_percentage(i_id) / 100 ) ) * TO_NUMBER(i_odd) ), 2);  
    
       IF(odd < 1) THEN  
                ret_value := TO_NUMBER(i_odd);  
              ELSE  
                ret_value := to_char(odd,'9999.00');  
              END IF;  
    
          END IF;  
    
          RETURN to_char(ret_value,'9999.00');  
    
    END getOdds;