SQLServer字符串前后补0的方法,RPAD,LPAD,REPLICATE的使用
(2012-01-19 09:20:53)
标签:
sqlit |
分类: 资源 |
http://www.zbitedu.com/?action-viewthread-tid-14734
[sql]if exists (select 1 from
dbo.sysobjects where id=object_id(N'tmp') and
OBJECTPROPERTY(id,N'IsUserTable')=1)
drop table tmp;
CREATE TABLE tmp (
id int DEFAULT NULL,
txt varchar(20) DEFAULT NULL
);
insert into tmp values(22,null);
-- 补位函数
select cast(id as varchar)+REPLICATE('0',8-len(id)) from tmp;
select REPLICATE('0',8-len(id))+cast(id as varchar) from tmp;
[/sql]
drop table tmp;
CREATE TABLE tmp (
id int DEFAULT NULL,
txt varchar(20) DEFAULT NULL
);
insert into tmp values(22,null);
-- 补位函数
select cast(id as varchar)+REPLICATE('0',8-len(id)) from tmp;
select REPLICATE('0',8-len(id))+cast(id as varchar) from tmp;
[/sql]
前一篇:excel 报表