问题描述
最近我在 SQL Server 中使用 ISNUMERIC,当我遇到一个问题,导致找到了这段代码.
Recently I was working with ISNUMERIC in SQL Server, when I encountered a problem, which led to finding this snippet of code.
SELECT ISNUMERIC('.')
这返回 1,就像 true 一样,这不应该像 false 一样返回 0?
This returns 1, as in true, shouldn't this return 0 as in false?
推荐答案
SELECT CAST('.' AS MONEY)
返回 0.00
(尽管 int
和 float
的转换失败)
returns 0.00
(though the cast fails for int
and float
)
ISNUMERIC
只是检查该值是否可以转换为任何一种通常无用的数字数据类型.通常你想知道它是否可以转换为特定类型.
ISNUMERIC
just checks that the value can be cast to any one of the numeric datatypes which is generally useless. Usually you want to know whether it can be cast to a specific type.
此外,它甚至似乎无法针对所有可能的输入正确执行该任务..尽管成功转换为 int 和 money,ISNUMERIC(' ')
返回 0
.相反 ISNUMERIC(N'8')
返回 1
但没有成功转换为我尝试过的任何内容.
Additionally it doesn't even seem to do that task correctly for all possible inputs.. ISNUMERIC(' ')
returns 0
despite casting successfully to both int and money. Conversely ISNUMERIC(N'8')
returns 1
but does not cast successfully to anything that I tried.
这里有一些有用的辅助函数 IsNumeric, IsInt, IsNumber.
Some useful helper functions for that are here IsNumeric, IsInt, IsNumber.
SQL Server 2012 引入了TRY_PARSE
和 TRY_CONVERT
有助于解决这个问题大大.
SQL Server 2012 introduced TRY_PARSE
and TRY_CONVERT
that help with this greatly.
这篇关于为什么 ISNUMERIC('.') 返回 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!