SQL Server 中 IsInteger 的最佳等效项 [英] Best equivalent for IsInteger in SQL Server
问题描述
在 SQL Server (2000/2005/2008) 中,确定字段值是否为整数的最佳方法是什么?
What is the best way to determine whether or not a field's value is an integer in SQL Server (2000/2005/2008)?
IsNumeric 为各种不太可能转换为整数的格式返回 true.示例包括15,000"和15.1".
IsNumeric returns true for a variety of formats that would not likely convert to an integer. Examples include '15,000' and '15.1'.
您可以使用 like 语句,但它似乎只适用于具有预定位数的字段...
You can use a like statement but that only appears to work well for fields that have a pre-determined number of digits...
select * where zipcode like '[0-9][0-9][0-9][0-9][0-9]'
我可以编写一个用户定义的函数,尝试在 try/catch 块中将 varchar 参数转换为 int,但我正在与社区核实,看看是否有人遇到过任何简洁的方法来实现这一目标——最好是可以在 SQL 语句的 where 子句中使用而无需创建其他对象.
I could write a user defined function that attempts to convert a varchar parameter to an int within a try/catch block but I'm checking with the community to see if someone has come across any succient methods to achieve this goal - preferably one that can be used within the where clause of a SQL statement without creating other objects.
推荐答案
1 方法是
zipcode NOT LIKE '%[^0-9]%'
双重否定,必须爱他们!
Double negatives, got to love 'em!
这篇关于SQL Server 中 IsInteger 的最佳等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!