PadLeft 和 PadRight

1 PadLeft

即:向已知字符串左边补充字符,使整个字符串到达指定长度

 CREATE FUNCTION PadLeft
(
@VarOrgan varchar(10),/*原始字符*/
@TotalLength int,/*总长度*/
@Placeholder varchar(1)/*占位符*/
)
RETURNS varchar(100)
AS
BEGIN
declare @VarNew varchar(100),
@PadLen int
SET @VarNew=@VarOrgan
SET @PadLen=@TotalLength-LEN(@VarOrgan)
while(@PadLen>0)
begin
select @VarNew=@Placeholder+@VarNew
set @PadLen=@PadLen-1
end
return @VarNew
END

2 PadRight

即:向已知字符串右边补充字符,使整个字符串到达指定长度

 CREATE FUNCTION PadRight
(
@VarOrgan varchar(10),/*原始字符*/
@TotalLength int,/*总长度*/
@Placeholder varchar(1)/*占位符*/
)
RETURNS varchar(100)
AS
BEGIN
declare @VarNew varchar(100),
@PadLen int
SET @VarNew=@VarOrgan
SET @PadLen=@TotalLength-LEN(@VarOrgan)
while(@PadLen>0)
begin
select @VarNew=@VarNew+@Placeholder
set @PadLen=@PadLen-1
end
return @VarNew
END

3 测试结果

SELECT dbo.PadLeft('',6,'') PadLeft,
dbo.PadRight('',4,'') PadRight

PadLeft 和 PadRight

上一篇:php基础_2


下一篇:Linux内核分析作业第四周