【解决】Oracle关联查询invalid number隐式转换问题

在SQL查询语句中出现invalid number问题,有两种猜测可能

1.普通单表查询中出现问题

select * from table where id = 'abc'

当我的 id 为 number 型,这样加条件会报错

2.复杂关联表查询中出现问题,除了第 1 点原因得错误之外,还可能会因为关联表的相关联字段一个是字符串型一个是数字型

select t1.* ,t2.* from t1
left join t2
on t1.id = t2.code
where t1.id = '123'

t1.id 是 number 型

t2.code 是 varchar 型

在 on 条件时会报错,因为会默认把 t2.code 隐式转换为 number,即 invalid number

所以应该使用 to_char(t1.id) 的方法将 id 转换为 varchar 去与 code 关联

另外:字符串类型查询出来靠左,数字型靠右

原创文章 88 获赞 41 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Damionew/article/details/103023344