SQL 在 Oracle 中如何判断一个值是否不是数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28819709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you tell if a value is not numeric in Oracle?
提问by Kyle Williamson
I have the following code that returns an error message if my value is invalid. I would like to give the same error message if the value given is not numeric.
如果我的值无效,我有以下代码返回错误消息。如果给定的值不是数字,我想给出相同的错误消息。
IF(option_id = 0021) THEN
IF((value<10000) or (value>7200000) or /* Numeric Check */)THEN
ip_msg(6214,option_name); -- Error Message
return;
END IF;
END IF;
In SQL Server, I simply used ISNUMERIC()
. I would like to do something similar in Oracle. Such as,
在 SQL Server 中,我只是使用ISNUMERIC()
. 我想在 Oracle 中做类似的事情。如,
IF((!ISNUMERIC(value)) or (value<10000) or (value>7200000))
THEN ...
回答by Rob van Laarhoven
REGEXP_LIKE(column, '^[[:digit:]]+$')
returns TRUE if column holds only numeric characters
如果列仅包含数字字符,则返回 TRUE
回答by Lukasz Szozda
From Oracle DB 12c Release 2
you could use VALIDATE_CONVERSIONfunction:
从Oracle DB 12c Release 2
您可以使用VALIDATE_CONVERSION功能:
VALIDATE_CONVERSION determines whether expr can be converted to the specified data type. If expr can be successfully converted, then this function returns 1; otherwise, this function returns 0. If expr evaluates to null, then this function returns 1. If an error occurs while evaluating expr, then this function returns the error.
VALIDATE_CONVERSION 确定 expr 是否可以转换为指定的数据类型。如果可以成功转换expr,则该函数返回1;否则,此函数返回 0。如果 expr 计算结果为 null,则此函数返回 1。如果计算 expr 时发生错误,则此函数返回错误。
IF (VALIDATE_CONVERSION(value AS NUMBER) = 1) THEN
...
END IF;
回答by Justin Cave
There is no built-in function. You could write one
没有内置函数。你可以写一个
CREATE FUNCTION is_numeric( p_str IN VARCHAR2 )
RETURN NUMBER
IS
l_num NUMBER;
BEGIN
l_num := to_number( p_str );
RETURN 1;
EXCEPTION
WHEN value_error
THEN
RETURN 0;
END;
and/or
和/或
CREATE FUNCTION my_to_number( p_str IN VARCHAR2 )
RETURN NUMBER
IS
l_num NUMBER;
BEGIN
l_num := to_number( p_str );
RETURN l_num;
EXCEPTION
WHEN value_error
THEN
RETURN NULL;
END;
You can then do
然后你可以做
IF( is_numeric( str ) = 1 AND
my_to_number( str ) >= 1000 AND
my_to_number( str ) <= 7000 )
If you happen to be using Oracle 12.2 or later, there are enhancements to the to_number
function that you could leverage
如果您碰巧使用 Oracle 12.2 或更高版本,则to_number
可以利用该功能的增强功能
IF( to_number( str default null on conversion error ) >= 1000 AND
to_number( str default null on conversion error ) <= 7000 )
回答by David Faber
You can use the following regular expression which will match integers (e.g., 123
), floating-point numbers (12.3
), and numbers with exponents (1.2e3
):
您可以使用以下正则表达式来匹配整数(例如123
)、浮点数 ( 12.3
) 和带指数的数字 ( 1.2e3
):
^-?\d*\.?\d+([eE]-?\d+)?$
If you want to accept +
signs as well as -
signs (as Oracle does with TO_NUMBER()
), you can change each occurrence of -
above to [+-]
. So you might rewrite your block of code above as follows:
如果您想同时接受+
符号和-
符号(就像 Oracle 对 所做的那样TO_NUMBER()
),您可以将-
上面的每个出现更改为[+-]
。因此,您可以按如下方式重写上面的代码块:
IF (option_id = 0021) THEN
IF NOT REGEXP_LIKE(value, '^[+-]?\d*\.?\d+([eE][+-]?\d+)?$') OR TO_NUMBER(value) < 10000 OR TO_NUMBER(value) > 7200000 THEN
ip_msg(6214,option_name);
RETURN;
END IF;
END IF;
I am not altogether certain that would handle all values so you may want to add an EXCEPTION
block or write a custom to_number()
function as @JustinCave suggests.
我并不完全确定会处理所有值,因此您可能希望按照@JustinCave 的建议添加一个EXCEPTION
块或编写一个自定义to_number()
函数。
回答by TechDo
The best answer I found on internet:
我在互联网上找到的最佳答案:
SELECT case when trim(TRANSLATE(col1, '0123456789-,.', ' ')) is null
then 'numeric'
else 'alpha'
end
FROM tab1;