【问题标题】:How to convert a string to number in PL/SQL如何在 PL/SQL 中将字符串转换为数字
【发布时间】:2012-01-13 19:35:24
【问题描述】:

我有 5 个字符串,可以包含数字、小数点、字母和空格。如果字符串中的所有字符都是数字,我想将此字符串转换为数字(整数)。即

  • 不允许小数点
  • 不允许 +/- 符号
  • 中间不允许有空格,但可以在极端情况下允许使用空格

提前致谢。

【问题讨论】:

    标签: database string plsql numbers


    【解决方案1】:

    也可以试试这个

    select floor(to_number(TRANSLATE(' +1234.34','+-',' '))) from dual;
    

    假设 +1234.34 是输入

    【讨论】:

      【解决方案2】:
      create or replace function is_int(p_str in varchar2) return number as
      begin
        if regexp_instr(p_str, '^[[:space:]]*[[:digit:]]{1,5}[[:space:]]*$') > 0 then
          return 1;
        end if;
      
        return 0;
      end;
      /
      show errors
      
      with strings as (
        select '12345' as string from dual
        union all
        select '1234' as string from dual
        union all
        select '123' as string from dual
        union all
        select '12' as string from dual
        union all
        select '1' as string from dual
        union all
        select '01' as string from dual
        union all
        select '' as string from dual
        union all
        select '  345' as string from dual
        union all
        select '123  ' as string from dual
        union all
        select '12.45' as string from dual
        union all
        select '12 45' as string from dual
        union all
        select '12,45' as string from dual
        union all
        select '-1234' as string from dual
        union all
        select '+1234' as string from dual
        union all
        select 'A2345' as string from dual
      )
      select testcase, to_number(string)
      from strings
      where is_int(string) = 1
      ;
      
        TESTCASE TO_NUMBER(STRING)
      ---------- -----------------
               1             12345
               2              1234
               3               123
               4                12
               5                 1
               6                 1
               8               345
               9               123
      
      8 rows selected.
      
      create or replace function to_int(p_str in varchar2) return number as
      begin
        if regexp_instr(p_str, '^[[:space:]]*[[:digit:]]{1,5}[[:space:]]*$') > 0 then
          return to_number(p_str);
        end if;
      
        return null;
      end;
      /
      show errors
      
      with strings as (
        select 1 as testcase, '12345' as string from dual
        union all
        select 2, '1234' as string from dual
        union all
        select 3, '123' as string from dual
        union all
        select 4, '12' as string from dual
        union all
        select 5, '1' as string from dual
        union all
        select 6, '01' as string from dual
        union all
        select 7, '' as string from dual
        union all
        select 8, '  345' as string from dual
        union all
        select 9, '123  ' as string from dual
        union all
        select 10, '12.45' as string from dual
        union all
        select 11, '12 45' as string from dual
        union all
        select 12, '12,45' as string from dual
        union all
        select 13, '-1234' as string from dual
        union all
        select 14, '+1234' as string from dual
        union all
        select 15, 'A2345' as string from dual
      )
      select testcase, '''' || string || '''' as string
      from strings
      where to_int(string) is not null
      ;
      
        TESTCASE STRING
      ---------- ---------------------
               1 '12345'
               2 '1234'
               3 '123'
               4 '12'
               5 '1'
               6 '01'
               8 '  345'
               9 '123  '
      
      8 rows selected.
      

      【讨论】:

        【解决方案3】:

        假设您正在使用变量foo_code

        IF TRIM(TRANSLATE(TRANSLATE(TRIM(foo_code), ' ', 'x'), '0123456789', ' ')) IS NULL THEN
          foo_number := TO_NUMBER(foo_code);
        END IF;
        

        分解:

        • 修剪前导和尾随空格
        • 将任何内部空格转换为“x”——想想测试用例“1234 098”(即打破第三个条件的简单字符串)
        • 将任何数字转换为空格
        • 修剪前导和尾随空格
        • 如果所有内容都是数字,则应留下一个空字符串,在 Oracle 术语中为 NULL

        【讨论】:

          【解决方案4】:

          to_number 函数将字符串转换为数字。

          【讨论】:

          • 如何在上述条件下检查字符串的有效性?
          • 在转换之前使用 Instr 函数检查字符串中是否包含无效字符。
          【解决方案5】:

          在 PL/SQL 中使用To_Number Function 将字符串转换为数字,示例如下。

          to_number('1210.73', '9999.99') would return the number 1210.73 
          to_number('546', '999') would return the number 546 
          to_number('23', '99') would return the number 23 
          

          编辑:

          在 PL/SQL 中,您可以使用 LENGTHTRIMTRANSLATE 函数检查字符串是否由数字字符组成。

          LENGTH(TRIM(TRANSLATE(string1, ' +-.0123456789', ' ')))
          

          【讨论】:

          • 如果字符串中有字母、小数点,我只想将其转换为整数,前提是字符串中存在数字。
          【解决方案6】:

          您尝试过 CAST(var AS NUMBER) 吗?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-18
            • 2018-12-20
            • 1970-01-01
            • 2016-12-07
            • 1970-01-01
            • 2019-07-03
            相关资源
            最近更新 更多