小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

向左填充指定字符串

 zww_blog 2012-03-15

向左填充指定字符串

分類: SQL函數(shù)分享系列 502人閱讀 評論(1) 收藏 舉報(bào)

go

--創(chuàng)建函數(shù)(該函數(shù)來自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'

 

select dbo.padleft(id,2,'0') as id from @table

 

--運(yùn)行結(jié)果

/*

id

-------

01

02

03

04

05

06

*/

 

go

--創(chuàng)建函數(shù)(第二版)(作者:maco_wang)

create function padleftV2

(

    @sql varchar(200),  --需填充的字符串

    @char varchar(4),   --填充使用的字符

    @len int            --填充后的長度

)

returns varchar(200)

as

begin

return (right(replicate(@char,@len)+@sql,@len))

end

go

--測試示例

declare @table table(id int)

insert into @table(id)

select 1 union all

select 3 union all

select 6

 

select dbo.padleftV2(cast(id as varchar),'0',10) as id from @table

--運(yùn)行結(jié)果

/*

id

-------------

0000000001

0000000003

0000000006

*/

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約