检查字符是否为数字的最快方法?

发布于 2024-12-06 01:37:48 字数 998 浏览 2 评论 0原文

我遇到了 sqlserver 的 ISNUMERIC 函数的问题,它为“,”返回 true

我正在解析邮政编码并尝试查看第二个字符(应该是数字)是否为 0,并在每种情况下执行不同的操作。问题是我不能只通过首先检查 isNumeric 来转换字符。下面是我的标量值函数的代码,用于返回第二个字符位置中的数字,如果不是数字则返回 -1。

@declare firstDigit int

IF ISNUMERIC(SUBSTRING(@postal,2,1) AS int) = 1
   set @firstDigit = CAST(SUBSTRING(@postal,2,1) AS int)
ELSE
   set @firstDigit = -1       

RETURN @firstdigit

因为当邮政编码不太有效时,此操作会失败。我只是想找出如何检查 nvarchar @postal 的第二个字符是否是 0-9 之间的数字。我见过不同类型的解决方案,例如使用 LIKE [0-9] 或使用 PATINDEX 等。

是否有更好/更简单的方法来做到这一点,以及如果不是哪种方法最快?

编辑:根据 Aaron Bertrand 的建议添加代码

ON z.postal = 
   CASE
      WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
          THEN SUBSTRING(v.patientPostal, 2,1) END) = 0 then v.patientPostal
      WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
          THEN SUBSTRING(v.patientPostal, 2,1) END) > 0 then LEFT(v.patientPostal,3)

I am having issues with sqlserver's ISNUMERIC function where it is returning true for ','

I am parsing a postal code and trying to see if the second char (supposed to be a digit) is a 0 or not and do something different in each case. The issue is that I can't just cast the char by checking isNumeric first. Here is the code for my scalar-valued function to return the digit in the second char location, and -1 if it is not a digit.

@declare firstDigit int

IF ISNUMERIC(SUBSTRING(@postal,2,1) AS int) = 1
   set @firstDigit = CAST(SUBSTRING(@postal,2,1) AS int)
ELSE
   set @firstDigit = -1       

RETURN @firstdigit

Since this fails when the postal code is not quite valid. I am just trying to find out how to check if the nvarchar @postal 's second character is a digit from 0-9. I have seen different types of solutions such as using LIKE [0-9] or using PATINDEX etc.

Is there a better/easier way to do this, and if not which method will be the fastest?

EDIT: Code added as per Aaron Bertrand's suggestion

ON z.postal = 
   CASE
      WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
          THEN SUBSTRING(v.patientPostal, 2,1) END) = 0 then v.patientPostal
      WHEN CONVERT(INT, CASE WHEN SUBSTRING(v.patientPostal,2,1) LIKE '[0-9]' 
          THEN SUBSTRING(v.patientPostal, 2,1) END) > 0 then LEFT(v.patientPostal,3)

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

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

发布评论

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

评论(3

樱桃奶球 2024-12-13 01:37:48

如果您能够发现 WHERE col LIKE '[0-9]' 与您提出的任何其他方法之间的任何差异,我会感到非常惊讶。但我同意 Denis 的观点,将其放在一个函数中,以便您在所有代码中一致地使用相同的检查(或者至少,如果您由于大量扫描等原因而避免 UDF,请在代码中放置一个标记,该标记将以便以后更容易进行大规模更改)。

也就是说,与在函数内部使用解析方法相比,仅使用标量 UDF 肯定会看到更多的性能损失。您确实应该比较 UDF 的性能与使用 CASE 内联执行该操作的性能。例如

SELECT Postal = CONVERT(INT, CASE WHEN SUBSTRING(postal,2,1) LIKE '[0-9]' 
       THEN SUBSTRING(postal, 2,1) END)
FROM ...

,如果字符不是数字,这将产生NULL

如果您只处理检查局部变量,那么使用什么解析方法实际上并不重要,您最好将优化工作集中在其他地方。

编辑向演示的JOIN子句添加建议。这可能会导致更少的持续扫描,但更具可读性(更少的子字符串调用等):

;WITH v AS 
(
    SELECT /* other columns, */ patientPostal, 
      ss = SUBSTRING(v.patientPostal,2,1),
      FROM [whatever table is aliased v in current query]
)
SELECT /* column list */
FROM [whatever table is aliased z in current query]
INNER JOIN v ON z.postal = CONVERT(INT, CASE 
    WHEN v.ss = '0' THEN ss
    WHEN v.ss LIKE '[1-9]' THEN LEFT(v.patientPostal, 3)
END);

I'd be very surprised if you would ever be able to detect any difference between WHERE col LIKE '[0-9]' and any other methods you come up with. But I agree with Denis, put that away in a function so that you use the same check consistently throughout all your code (or at least, if you're avoiding UDFs because of large scans etc., put a marker in your code that will make it easy to change on a wide scale later).

That said, you are most certainly going to see more of a performance hit just by using a scalar UDF than what method you use to parse inside the function. You really ought to compare performance of the UDF vs. doing that inline using CASE. e.g.

SELECT Postal = CONVERT(INT, CASE WHEN SUBSTRING(postal,2,1) LIKE '[0-9]' 
       THEN SUBSTRING(postal, 2,1) END)
FROM ...

This will yield NULL if the character is not numeric.

If you are only dealing with checking local variables, it really is not going to matter what parsing method you use, and you are better off focusing your optimization efforts elsewhere.

EDIT adding suggestion to demonstrated JOIN clause. This will potentially lead to less constant scans but is a lot more readable (far fewer substring calls etc):

;WITH v AS 
(
    SELECT /* other columns, */ patientPostal, 
      ss = SUBSTRING(v.patientPostal,2,1),
      FROM [whatever table is aliased v in current query]
)
SELECT /* column list */
FROM [whatever table is aliased z in current query]
INNER JOIN v ON z.postal = CONVERT(INT, CASE 
    WHEN v.ss = '0' THEN ss
    WHEN v.ss LIKE '[1-9]' THEN LEFT(v.patientPostal, 3)
END);
太阳哥哥 2024-12-13 01:37:48

最好的方法是这样的:

IF SUBSTRING(@postal,2,1) LIKE [0-9]
CAST(SUBSTRING(@postal,2,1) AS int)

The best way to do it is this:

IF SUBSTRING(@postal,2,1) LIKE [0-9]
CAST(SUBSTRING(@postal,2,1) AS int)
皇甫轩 2024-12-13 01:37:48

看看 IsNumeric、IsInt、IsNumber 它有检查这 3 种类型

Take a look at IsNumeric, IsInt, IsNumber it has checks for those 3 types

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