SQL Server JOIN 中的 IsNumeric

发布于 2024-12-27 11:38:06 字数 432 浏览 0 评论 0原文

我的问题似乎很简单,但我被困在这里。我有一个表,其中有一个名为“SrcID”的“nvarchar”列,我在其中存储数字和字符串。现在,当我尝试在“加入”条件下检查该列上的“IsNumeric”时,如下所示,

   ISNUMERIC(SrcID) = 1 AND SrcID > 15

我收到以下错误:

  Msg 245, Level 16, State 1, Line 47
  Conversion failed when converting the nvarchar value 'Test' to data type int.

令人惊讶的是,当我删除检查“SrcID > 15”时,我的查询正在运行适当地。我应该在此声明中添加其他内容吗?

请帮助我解决这个问题。提前致谢!!

My problem seems to be very simple but I'm stuck here. I have a table which has an "nvarchar" column called "SrcID" and I store both numbers and strings in that. Now, when I try to check for "IsNumeric" on that column in a "Join" condition, something like below,

   ISNUMERIC(SrcID) = 1 AND SrcID > 15

I am getting the following error:

  Msg 245, Level 16, State 1, Line 47
  Conversion failed when converting the nvarchar value 'Test' to data type int.

Amazingly, when I remove the check "SrcID > 15", my query is running properly. Should I include anything else in this statement?

Please help me in fixing the issue. Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2025-01-03 11:38:06

您不能指望数据库计算过滤表达式的顺序。有一个查询优化器将评估您的 SQL 并根据它认为会产生最佳性能的情况构建执行查询的计划。

在这种情况下,IsNumeric() 不能与索引一起使用,这意味着对表中的每一行运行一个函数。因此,它几乎永远不会提供最佳的感知性能。将此与 SrcID > 进行比较15 表达式,可以与索引(如果存在)匹配,并且即使不存在也只是单个运算符表达式。它还可用于过滤 IsNumeric() 函数需要运行的潜在行数。

您可以使用视图、子查询、CTE、CASE 语句或计算列来解决这个问题。这是一个 CTE 示例:

With NumericOnly As 
(
    SELECT <columns> FROM MyTable WHERE IsNumeric(SrcID) = 1
)
SELECT <columns> FROM NumericOnly WHERE SrcID > 15

这是一个 CASE 语句选项:

SELECT <columns> FROM MyTable WHERE CASE WHEN IsNumeric(SrcIC) = 1 THEN Cast(SrcID As Int) ELSE 0 END > 15

You can't count on the order in which a database will evaluate filtering expressions. There is a query optimizer that will evaluate your SQL and build a plan to execute the query based on what it perceives will yield the best performance.

In this context, IsNumeric() cannot be used with an index, and it means running a function against every row in the table. Therefore, it will almost never provide the best perceived performance. Compare this with the SrcID > 15 expression, which can be matched with an index (if one exists), and is just a single operator expression even if one doesn't. It can also be used to filter down the number of potential rows where the IsNumeric() function needs to run.

You can likely get around this with a view, a subquery, a CTE, a CASE statement, or a computed column. Here's a CTE example:

With NumericOnly As 
(
    SELECT <columns> FROM MyTable WHERE IsNumeric(SrcID) = 1
)
SELECT <columns> FROM NumericOnly WHERE SrcID > 15

And here's a CASE statement option:

SELECT <columns> FROM MyTable WHERE CASE WHEN IsNumeric(SrcIC) = 1 THEN Cast(SrcID As Int) ELSE 0 END > 15
太阳公公是暖光 2025-01-03 11:38:06

WHERE 子句中的过滤器不按任何特定顺序进行计算。

这是 SQL Server 的一个常见误解 - 优化器将检查它认为最快的条件/最简单,并尝试以最有效的方式限制数据。

在您的示例中,您可能在 SrcID 上有一个索引,并且优化器认为首先将结果限制为 SrcID > 的位置会更快。 15,然后在所有这些行上运行该函数(因为该函数需要检查每一行,否则)。

您可以尝试使用括号强制执行操作顺序,例如:

WHERE (ISNUMERIC(SrcID) = 1) AND SrcID > 15

或者使用 case 语句:

WHERE CASE WHEN ISNUMERIC(SrcID) = 1 THEN SrcID > 15 否则 1=0 结束

The filters in a WHERE clause are not evaluated in any particular order.

This is a common misconception with SQL Server - the optimizer will check whichever conditions it thinks it can the fastest/easiest, and try to limit the data in the most efficient way possible.

In your example, you probably have an index on SrcID, and the optimizer thinks it will be quicker to FIRST limit the results to where the SrcID > 15, then run the function on all those rows (since the function will need to check every single row otherwise).

You can try to force an order of operations with parentheses like:

WHERE (ISNUMERIC(SrcID) = 1) AND SrcID > 15

Or with a case statement:

WHERE CASE WHEN ISNUMERIC(SrcID) = 1 THEN SrcID > 15 ELSE 1=0 END

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文