您当前的位置: 首页 >  sql

[MSSQL]向左填充指定字符串

发布时间:2012-02-14 08:21:33 ,浏览量:0

 
go

--创建函数(该函数来自csdn,作者不详)
create function [dbo].[padleft]
(
@str varchar(50), --需要填充的字符串
@totalwidth int, --填充后的长度
@paddingchar char(1)--填充使用的字符
)
returns varchar(1000) as 
begin 
declare @s varchar(100)
set @s = @str
if ( len(@str) < @totalwidth)
begin
declare @i int
declare @strlen int
declare @temp varchar(100)
set @i = 1; 
set @strlen = @totalwidth - len(@str)
set @temp = '';
while(@i <= @strlen )
begin
set @temp = @temp + @paddingchar;
set @i = @i + 1;
end
set @s = @temp + @str
end
return (@s)
end
go
--测试示例
declare @table table (id nvarchar(20))
insert into @table
select '1' union all
select '2' union all
select '3' union all
select '4' union all
select '5' union all

select '6'
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    106347博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2696s