Sql 将列解析为 int 并检查它是否在范围内
我使用了此处中投票最高的答案来检查单元格值是否是一个整数。但我还需要对其进行范围检查。
SELECT * FROM Table
WHERE (dbo.IsInteger(FieldName) = 1) AND FieldName > 400 AND FieldName < 500
但这会返回转换错误,因为列中的某些单元格包含文本。那么,是否有可能获得一个子集,即仅来自 IsInteger
查询的结果,然后对结果进行范围检查?
I've used the top voted answer from here to check if a cell value is an integer. But I also need to do a range check on it.
SELECT * FROM Table
WHERE (dbo.IsInteger(FieldName) = 1) AND FieldName > 400 AND FieldName < 500
But this returns a conversion error, as some of the cells in the column contains text. So, is it possible to get a subset, that is only results from the IsInteger
query, and then do a range check on the result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将其包装在
CASE
表达式中。我稍微更改了查询以使用BETWEEN
以避免重复表达式。当
dbo.IsInteger(FieldName) <> 时,表达式的结果将为
与NULL
。 1BETWEEN
谓词不匹配。当然,这是完全不可控制的,但您的原始查询也是如此。
You need to wrap it in a
CASE
expression. I've altered the query slightly to useBETWEEN
to avoid having to repeat the expression.The result of the expression will be
NULL
whendbo.IsInteger(FieldName) <> 1
which will not match theBETWEEN
predicate.Of course this is completely unsargable but so is your original query.