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

May*_*yur 12 database string plsql numbers

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

  • 不允许小数点
  • 不允许+/-标志
  • 中间不允许有空格,但可以允许它们处于极端状态

提前致谢.

Sai*_*ala 17

在PL/SQL中使用To_Number函数将字符串转换为数字,例如,请参见下文.

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 
Run Code Online (Sandbox Code Playgroud)

编辑:

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

LENGTH(TRIM(TRANSLATE(string1, ' +-.0123456789', ' ')))
Run Code Online (Sandbox Code Playgroud)


Sti*_*sis 0

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