SQL:在字符串有小数时,如何将字符串转换为int
我试图根据员工的年龄在两个桌子上进行内部连接,但是一张桌子的年龄没有十进制,一个桌子的年龄为小数。
例如:
表1年龄:26
表2年龄:26.0
我试图使用铸件,但要获得错误:
转换在将VARCHAR值'26 .0'转换为数据类型int。
时失败
这是导致错误的代码:
FROM Table1
INNER JOIN Table2
ON CAST(Table1.age As INT) = CAST(Table2.age As INT)
I am trying to do an inner join on two tables based on employees age, however one table has age without a decimal and one has it with a decimal.
EX:
Table 1 Age: 26
Table 2 Age: 26.0
I have tried to use CAST, but get the error:
Conversion failed when converting the varchar value '26.0' to data type int.
Here is my code that cause the error:
FROM Table1
INNER JOIN Table2
ON CAST(Table1.age As INT) = CAST(Table2.age As INT)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先转换为十进制,然后转换为int;或(字符串)在施放到int之前,用''替换'.0'。
You can first convert to decimal and then convert to int; or (string) replace '.0' with '' before casting to int.