求一条简单的SQL语句,谢谢!
CRNo CardNo
===============
3 1562
15 532
如上,就是CRNo,CardNo字段均要4位,不够的在前补0,也就是得到如下的结果:
CRNo CardNo
===============
0003 1562
0015 0532
------解决方案--------------------update tablename
set CRNo=right( '0000 '+CRNo,4),CardNo=right( '0000 '+CardNo,4)
------解决方案--------------------如果这两个字段都是字符类型的话:
select Right( '0000 ' + CRNo, 4) as ACRNO, Right( '0000 ' + CardNo, 4) as ACardNo
from 数据表
------解决方案--------------------if object_id( 'pubs..tb ') is not null
drop table tb
go
create table tb(CRNo int,CardNo int)
insert into tb(CRNo,CardNo) values(3 , 1562)
insert into tb(CRNo,CardNo) values(15, 532 )
go
select right( '0000 '+cast(CRNo as varchar),4) CRNo , right( '0000 '+cast(CardNo as varchar),4) CardNo from tb
drop table tb
/*
CRNo CardNo
-------- --------
0003 1562
0015 0532
(所影响的行数为 2 行)
*/