Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何在PL/SQL中将字符串转换为数字_Database_String_Plsql_Numbers - Fatal编程技术网

Database 如何在PL/SQL中将字符串转换为数字

Database 如何在PL/SQL中将字符串转换为数字,database,string,plsql,numbers,Database,String,Plsql,Numbers,我有5个字符串,可以有数字、小数点、字母和空格。如果字符串中的所有字符都是数字,我想将该字符串转换为数字(整数)。i、 e 不允许使用小数点 不允许使用+/-符号 中间不允许有空格,但在极端情况下允许有空格 提前感谢。您是否尝试使用CAST(变量作为数字)?在PL/SQL中使用将字符串转换为数字,请参见下面的示例 to_number('1210.73', '9999.99') would return the number 1210.73 to_number('546', '999') w

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

  • 不允许使用小数点
  • 不允许使用+/-符号
  • 中间不允许有空格,但在极端情况下允许有空格
提前感谢。

您是否尝试使用CAST(变量作为数字)?

在PL/SQL中使用将字符串转换为数字,请参见下面的示例

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中,可以使用、和函数检查字符串是否由数字字符组成

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

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

也可以尝试此方法

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

假设+1234.34是输入

假设您使用的是变量
foo_code

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

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

如果字符串中有字母、小数点,并且我只想在字符串中有数字的情况下将其转换为整数,该怎么办?如何在上述条件下检查字符串的有效性?在转换字符串之前,使用Instr函数检查字符串中是否有无效字符。
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.