数据库中用rpad转换字段的长度

来源:互联网 发布:股票每日收盘价数据 编辑:程序博客网 时间:2024/06/18 03:48

 

   在MySQL数据库中,有的时候用子查询时得出的数据和主语句中的字段不匹配。如下:

select codename  from ldcode where codetype='edortype'and code in(select edortype from LPEdorItem where EdorAcceptNo='6120150329000007')

子查询中,查出来的字段edortype是char(2),而code是char(20) ,他们是不匹配的,用trim(edortype)也不能解决问题。这时,应该使用rpad.

修改如下:

select codename  from ldcode where codetype='edortype'and code in(select  rpad(edortype,20 ,' ') from LPEdorItem where EdorAcceptNo='6120150329000007')

这时就可以正常的查出所需要的数据。

0 0
原创粉丝点击