检查字符是否为数字的最快方法?
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您能够发现
WHERE col LIKE '[0-9]'
与您提出的任何其他方法之间的任何差异,我会感到非常惊讶。但我同意 Denis 的观点,将其放在一个函数中,以便您在所有代码中一致地使用相同的检查(或者至少,如果您由于大量扫描等原因而避免 UDF,请在代码中放置一个标记,该标记将以便以后更容易进行大规模更改)。也就是说,与在函数内部使用解析方法相比,仅使用标量 UDF 肯定会看到更多的性能损失。您确实应该比较 UDF 的性能与使用
CASE
内联执行该操作的性能。例如,如果字符不是数字,这将产生
NULL
。如果您只处理检查局部变量,那么使用什么解析方法实际上并不重要,您最好将优化工作集中在其他地方。
编辑向演示的
JOIN
子句添加建议。这可能会导致更少的持续扫描,但更具可读性(更少的子字符串调用等):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.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):最好的方法是这样的:
The best way to do it is this:
看看 IsNumeric、IsInt、IsNumber 它有检查这 3 种类型
Take a look at IsNumeric, IsInt, IsNumber it has checks for those 3 types