在 Informix 中将 varchar 转换为数字
我在 Informix 中将 varchar 类型转换为 Int 类型时遇到问题。 实际上我不知道该值是否真的是 varchar 或不是我想转换为 INT 的值。这是一个沙箱系统。
例如: 我正在尝试运行这种类型,
Select telnumber from numbers n
where Cast(n.telnumber AS INT) between 1234 and 9999
但出现此错误: “字符到数字转换错误”
如果我像这样运行此查询:
Select telnumber from numbers n where n.telnumber between '1234' and '9999'
它会带来一个结果集,但不在我定义的范围内。
130987
130710
130723
如何将电话号码转换为数值并在 1234 和 9999 之间的范围内使用它
I have problem while converting varchar type to Int type in Informix.
Actually I don't know if the value is really varchar or not which I want to convert to INT. It's a sandbox system.
As Example:
I am trying to run this kind of
Select telnumber from numbers n
where Cast(n.telnumber AS INT) between 1234 and 9999
I got this error:
"Character to numeric conversion error"
If I run this query like this:
Select telnumber from numbers n where n.telnumber between '1234' and '9999'
it brings a resultset but not in the range that I defined.
130987
130710
130723
How can I convert telnumber to a numeric value and use it in "between" 1234 and 9999 range
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
转换错误表明 telnumber 列中的某些值未格式化为有效整数 - 因此会触发尝试转换时看到的错误。
第二个查询列出了额外的值,因为与字符串比较时“123”位于“10”和“20”之间。
如果您想将其限制为 4 位数字,那么您可以使用:
这仍会在结果集中包含“1AA2”。
遗憾的是,完整的正则表达式支持(例如 PCRE)并未作为 IDS 的标准提供。但是,非标准 MATCHES 运算符允许您这样做:
这是一个简单的正则表达式 - 但“*”是 shell 通配样式“零个或多个字符的任何序列”,而不是 Kleene Star“零个或多个字符”重复前一个字符”。
The conversion error suggests that some of the values in the telnumber column are not formatted as a valid integer - and therefore trigger the error you see when conversion is attempted.
The second query lists the extra values because '123' comes between '10' and '20' when compared as strings.
If you want to limit it to 4-digit numbers, then you could use:
This would still include '1AA2' in the result set.
Full regular expression support (such as PCRE) is not present as standard in IDS - sadly. However, the non-standard MATCHES operator would allow you to do it:
That is a simple regular expression - but the '*' is a shell globbing style 'any sequence of zero or more characters' rather than the Kleene Star 'zero or more repetitions of the previous character'.
简单回答:
Informix 内置了强制转换,您可以像这样使用它:
进一步:
但是,正如 Jonathan Leffler 之前指出的那样,听起来您的数据集包含不一定能够强制转换为整数的值。
如果是这样,那么这也许不是适合您目的的理想模式?
如果您绝对需要在此处包含非数字字符(例如,我怀疑您最终会得到“-”、“(”或“)”之一),您可以尝试排除与非数字字符匹配的行:
或者,您可以尝试使用存储过程,利用 ON EXCEPTION 块? (字符到数字转换错误为 errno 1213)。
有关使用“例外”的进一步阅读:http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls946.htm
进一步阅读关于定义和使用存储过程: http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls906.htm
Simple Answer:
Informix has built in casting, which you can use like this:
Further:
However, as Jonathan Leffler pointed out previously, it sounds like your dataset contains values which aren't necessarily cast-able to integer.
If that's so, then perhaps this isn't the ideal schema for your purposes?
If you absolutely need to have non-numeric characters in here (eg I suspect you'll end up with one of '-', '(' or ')'), you could try excluding rows that match non-numeric chars:
Alternatively, you could maybe try using a stored procedure, utilising an ON EXCEPTION block? (Character to numeric conversion error is errno 1213).
Further reading on using 'ON EXCEPTION': http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls946.htm
Further reading on defining and using stored procedures: http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls906.htm
我不使用 Informix,但这在 SQL Server 中对我有用:
但是,您可能会遇到问题,因为电话号码实际上是字符串而不是 INT,并且最终任何将它们视为 INT 的尝试都会失败。
I do not use Informix, but this works for me in SQL Server:
However, you will probably run into problems because telephone numbers really are strings and are not INTs, and ultimately any attempt to treat them as INTs will fail.
对我来说,这有效:
For me, this worked: