日期:2014-05-17 浏览次数:20587 次
create table #tmp(
nian smallint not null,
yue smallint not null,
autoinc int not null)
insert #tmp
select 2012,9,1
union all
select 2012,9,11
union all
select 2012,9,111
union all
select 2012,9,1111
union all
select 2012,10,222
union all
select 2012,10,22222
union all
select 2012,10,222222
------如何从#tmp表中得出以下结果(字符型)
2012090000001
2012090000011
2012090000111
2012090001111
2012100000222
2012100022222
2012100222222
create table #tmp(
nian smallint not null,
yue smallint not null,
autoinc int not null)
insert #tmp
select 2012,9,1
union all
select 2012,9,11
union all
select 2012,9,111
union all
select 2012,9,1111
union all
select 2012,10,222
union all
select 2012,10,22222
union all
select 2012,10,222222
SELECT CAST(nian AS varchar) + --年
CASE WHEN yue >= 1 AND yue <=9
THEN '0' + CAST(yue AS varchar)
ELSE CAST(yue AS varchar)
END + --月
CASE WHEN len(CAST(autoinc AS VARCHAR)) < ( SELECT max(len(CAST(autoinc AS varchar)))
FROM #tmp) --求autoinc的最大长度
THEN '0' + replicate('0',
(SELECT max(len(CAST(autoinc AS varchar))) FROM #tmp) - len(CAST(autoinc AS VARCHAR))
) --重复字符0
+ CAST(autoinc as varchar) &nb