检查 varchar 是否是数字
有没有一种简单的方法可以判断 varchar 是否是数字?
示例:
abc123 -->没有号码
123 -->是的,它是一个数字
Is there an easy way to figure out if a varchar is a number?
Examples:
abc123 --> no number
123 --> yes, its a number
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
要检查数字、货币和金额,请使用以下 SQL 片段。
为了快速获胜,请参考以下示例:
函数示例:
执行结果
To check the Number, Currency, and Amount, use the below SQL fragment.
For a quick win, refer to the below example:
Function example:
Execution result
ISNUMERIC 不会这样做 - 它告诉您字符串可以转换为任何数字类型,这几乎总是一条毫无意义的信息。例如,根据 ISNUMERIC,以下所有内容都是数字:
如果您想检查数字且仅检查数字,则需要使用负 LIKE 表达式:
ISNUMERIC will not do - it tells you that the string can be converted to any of the numeric types, which is almost always a pointless piece of information to know. For example, all of the following are numeric, according to ISNUMERIC:
If you want to check for digits and only digits, a negative LIKE expression is what you want:
ISNUMERIC 也可以
检查文章中的“注释”部分。
ISNUMERIC will do
Check the NOTES section too in the article.
你可以这样检查:
You can check like this:
使用 SQL Server 2012+,如果您有特定需求,可以使用 TRY_* 函数。例如,
Using SQL Server 2012+, you can use the TRY_* functions if you have specific needs. For example,
我遇到了允许十进制值的需要,因此我使用了
not Value like '%[^0-9.]%'
I ran into the need to allow decimal values, so I used
not Value like '%[^0-9.]%'
Wade73 对小数的回答 不'不太有效。我已将其修改为仅允许一个小数点。
Wade73's answer for decimals doesn't quite work. I've modified it to allow only a single decimal point.
Damien_The_Unknowner 指出他的唯一适合数字
Wade73 添加了位来处理小数点
neizan 进行了额外的调整没有发生。
不幸的是,似乎没有一个能够处理负值,并且它们似乎在值中存在逗号问题...
这是我的调整,以获取负值和带有逗号的值
Damien_The_Unbeliever noted that his was only good for digits
Wade73 added a bit to handle decimal points
neizan made an additional tweak as did notwhereuareat.
Unfortunately, none appear to handle negative values and they appear to have issues with a comma in the value...
Here's my tweak to pick up negative values and those with commas
Neizan 的代码 让值仅一个“。”通过。冒着过于迂腐的风险,我又添加了一个
AND
子句。Neizan's code lets values of just a "." through. At the risk of getting too pedantic, I added one more
AND
clause.不要忘记从数据中排除回车!
如:
Do not forget to exclude carriage returns from your data!
As in:
如果您想在字段上添加约束:
固定长度的正整数
In case you want to add a constraint on a field:
Positive integer with fixed length