如何过滤查询以仅显示列无法转换为 int 的结果
我有一个带有 varchar 列的表,其值应该是整数(遗留应用程序,在我到达公司之前构建的)。
我需要获取所有无法转换为整数的记录的列表,以便修复它们。
我该怎么做?
I have a table with a varchar column with values that should be integers (legacy application, built way before I got to the company).
I need to get a list of all the records that cannot be converted to integers, in order to fix them.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个简单的方法是:
假设您在转换时可以接受丢失小数。
A simple way is:
That assumes you are OK with losing decimals when you convert.
如果出于您的目的,分数或非标准表示形式(例如
1.3
或1E8
)可以被视为“可转换为 int”,则使用:If fractions or non-standard representations (like
1.3
or1E8
) can be considered "convertible to int" for your purposes, then use:ISNUMERIC 不会将其剪切为空字符串(= 零)或小数。
此否定搜索包含 0-9 范围之外的字符的任何内容。你给出了负整数吗?
ISNUMERIC does just not cut it for empty strings (= zero) or decimals.
This negative searches for anything that has characters outside the 0-9 range. Do you gave negative ints?
您可以尝试使用正则表达式。不确定您正在使用哪个 DBMS,但类似的东西可能适用于您的数据库。它应该在 some_column 中找到带有字符(大写/小写)大小写的记录。
SELECT * FROM sometable WHERE some_column SIMILAR TO '%[AZ | ] az]%';
You could try to use a regular expression. Not sure which DBMS you are using but something like this might work for your DB. It should find records with characters (upper/lower) case in some_column.
SELECT * FROM sometable WHERE some_column SIMILAR TO ‘%[A-Z | a-z]%’;