SQLServer PadLeft,PadRight
时间:2010-11-15 来源:一路前行
代码
Create Function PadLeft(@OriginalString varchar(20),@PaddingChar char(1),@TotalLength int)Returns Varchar(20) As
Begin
Declare @NewString varchar(20)
Select @NewString = IsNull(Replicate(@PaddingChar,@TotalLength - Len(isnull(@OriginalString ,0))), '') + @OriginalString
Return @NewString
End
Create Function PadRight(@OriginalString varchar(20),@PaddingChar char(1),@TotalLength int)
Returns Varchar(20) As
Begin
Declare @NewString varchar(20)
Select @NewString = @OriginalString + IsNull(Replicate(@PaddingChar,@TotalLength - Len(isnull(@OriginalString ,0))), '')
Return @NewString
End
调用如下,别忘记:dbo前缀
Select dbo.PadLeft('101', '0', 6) --002008Select dbo.PadRight('101', '0', 6) --200800
相关阅读 更多 +