如果一个字段的的数据是按顺序排列的整数:1 2 311 12。。,我要使它们变成四位数的值:0001 0002 00030011 0012。。我该怎么办?在sql server中有个取字符串相反的函数,我忘记了叫什么,就是你的123 会变成321 ,还有12变成21 然后就很简单了:Declare @iNum varchar(10) set @inum='13' select reverse(right(100000000+reverse(@inum),4))程序参考下面的方法:procedure MakeStrToSeven(SList:TStrings); const FILL='0000000'; var i,p1,p2:Integer; l,s1,s2:String; tsl:TStringList; begin tsl:=TStringList.Create; try for i:=0 to SList.Count-1 do begin l:=SList[i]; p1:=Pos('|',l); p1:=PosEx('|',l,p1+1); p2:=PosEx('|',l,p1+1); s1:=Copy(l,p1+1,p2-p1-1); if Length(s1)<7 then begin if tsl.IndexOfName(s1)>=0 then begin s2:=tsl.Values[s1]; s2:=IntToStr(StrToInt(s2)+1); end else begin s2:=s1+Copy(FILL,1,7-Length(s1)); tsl.Values[s1]:=s2; end; SList[i]:=Copy(l,1,p1)+s2+Copy(l,p2,Length(l)-p2); end; end; finally tsl.Free; end; end;
给你个例子做个参考 declare @str varchar(4), @i intselect @str=cast(序号字段 as varchar(4)) from 表 @i=len(@str) while i<4 begin SET @str='0'+@str set @i=@i+1 endselect @str from 表
然后就很简单了:Declare @iNum varchar(10)
set @inum='13'
select reverse(right(100000000+reverse(@inum),4))程序参考下面的方法:procedure MakeStrToSeven(SList:TStrings);
const
FILL='0000000';
var
i,p1,p2:Integer;
l,s1,s2:String;
tsl:TStringList;
begin
tsl:=TStringList.Create;
try
for i:=0 to SList.Count-1 do
begin
l:=SList[i];
p1:=Pos('|',l);
p1:=PosEx('|',l,p1+1);
p2:=PosEx('|',l,p1+1);
s1:=Copy(l,p1+1,p2-p1-1);
if Length(s1)<7 then
begin
if tsl.IndexOfName(s1)>=0 then
begin
s2:=tsl.Values[s1];
s2:=IntToStr(StrToInt(s2)+1);
end
else
begin
s2:=s1+Copy(FILL,1,7-Length(s1));
tsl.Values[s1]:=s2;
end;
SList[i]:=Copy(l,1,p1)+s2+Copy(l,p2,Length(l)-p2);
end;
end;
finally
tsl.Free;
end;
end;
declare
@str varchar(4),
@i intselect @str=cast(序号字段 as varchar(4)) from 表
@i=len(@str)
while i<4
begin
SET @str='0'+@str
set @i=@i+1
endselect @str from 表