日期:2014-05-18 浏览次数:20513 次
select right('00000' + cast(col as int),5) col from tb
------解决方案--------------------
create table tb(col varchar(5)) insert into tb values('1') insert into tb values('01') insert into tb values('001') insert into tb values('0001') insert into tb values('00001') insert into tb values('0') insert into tb values('00') insert into tb values('000') insert into tb values('0000') insert into tb values('00000') go select right('00000' + cast(cast(col as int) as varchar),5) col from tb drop table tb /* col ---------- 00001 00001 00001 00001 00001 00000 00000 00000 00000 00000 (所影响的行数为 10 行) */
------解决方案--------------------
select top 10000 id=identity( bigint,0,1) into tmp from syscolumns a,syscolumns b select right('00000' + cast(id as varchar) , 5) id from tmp drop table tmp /* id ---------- 00000 00001 00002 00003 00004 00005 00006 00007 */
------解决方案--------------------
表里有什么数据
要得到什么样的数据
表里有
1
01
001
0001
00001
是不是要得到
00001
00001
00001
00001
00001
------解决方案--------------------
update 表 set id=right('00000' + cast(id as varchar),5)
如果ID 字段为 INT
alter table 表 alter column id varchar(6)
update 表 set id=right('00000' + cast(id as varchar),5)
------解决方案--------------------
create table #(id varchar(6),pwd varchar(10)) insert into # select '0',left(checksum(newid()),6) union all select '1',left(checksum(newid()),6) insert into # select left('0'+id,5) as id,left(checksum(newid()),6) from # union all select left('00'+id,5),left(checksum(newid()),6) from # union all select left('000'+id,5),left(checksum(newid()),6) from # union all select left('0000'+id,5),left(checksum(newid()),6) from # union all select left('00000'+id,5),left(checksum(newid()),6) from # select * from #
------解决方案--------------------
--要插入新记录? declare @a table (id varchar(5)) declare @b table (id varchar(5)) insert @a select '0'+id id from [Table] where len(id)<5 insert [Table](id)select id from @a insert @b select id from @a while @@rowcount>0 begin delete from @a insert @a select '0'+id id from @b where len(id)<5 insert [Table](id)select id from @a delete from @b insert @b select id from @a end
------解决方案--------------------
create function [dbo].[zizeng](@id int) returns nvarchar(20) begin declare @str nvarchar(20) declare @str1 nvarchar(2) declare @str2 nvarchar(3) declare @str3 nvarchar(4) declare @str4 nvarchar(5) if len(@id)=1 begin set @str1='0'+convert(nvarchar(1),@id) set @str2='00'+convert(nvarchar(1),@id) set @str3='000'+convert(nvarchar(1),@id) set @str4='0000'+convert(nvarchar(1),@id) end else if len(@id)=2 begin set @str2='0'+convert(nvarchar(2),@id) set @str3='00'+convert(nvarchar(2),@id