sql server中为某个整数前加上一定数量的0,如1,要返回00001

select replace(space(5-Len(ltrim(str(field1)))),' ','0')+ltrim(str(field1))

select right('0000'+CONVERT(varchar(5),field1),5)

如:

DECLARE @MyCounter INT
set @MyCounter=3
select replace(space(5-Len(ltrim(str(@MyCounter)))),' ','0')+ltrim(str(@MyCounter))
select right('0000'+CONVERT(varchar(5),@MyCounter),5)

DECLARE @MyCounter INT
set @MyCounter=30
select replace(space(5-Len(ltrim(str(@MyCounter)))),' ','0')+ltrim(str(@MyCounter))
select right('0000'+CONVERT(varchar(5),@MyCounter),5)

DECLARE @MyCounter INT
set @MyCounter=600
select replace(space(5-Len(ltrim(str(@MyCounter)))),' ','0')+ltrim(str(@MyCounter))
select right('0000'+CONVERT(varchar(5),@MyCounter),5)

原文地址:https://www.cnblogs.com/anan/p/1262006.html