一个IF语句问题
描述:如果 【id】 在 0到10之间 则 令EipSubMumber=000 + 【id】
【id】在 10 到 100 之间 则 令EipSubMumber=00 + 【id】
【id】在 100 到 1000 之间 则 令EipSubMumber=0 + 【id】
【id】大于等于1000 则 令EipSubMumber=【id】
如 id=1 则EipSubMumber=0001
如 id=10 则EipSubMumber=0010
如 id=100 则EipSubMumber=0100
如 id=1000 则EipSubMumber=1000
declare @EipSubMumber int
if 10> @@IDENTITY > 0
set @EipSubMumber= '000 ' + @@IDENTITY
else
if 100> @@IDENTITY> =10
set @EipSubMumber= '00 ' + @@IDENTITY
else
if 1000> @@IDENTITY> =100
set @EipSubMumber= '0 ' + @@IDENTITY
else
if @@IDENTITY> =1000
set @EipSubMumber=@@IDENTITY
end if
end if
end if
end if
------解决方案--------------------declare @EipSubMumber varchar(10)
------解决方案--------------------declare @EipSubMumber int
if @@IDENTITY > =0 and @@IDENTITY <10
set @EipSubMumber= '000 ' + @@IDENTITY
else
if @@IDENTITY> =10 and 100> @@IDENTITY
set @EipSubMumber= '00 ' + @@IDENTITY
else
if @@IDENTITY> =100 and 1000> @@IDENTITY
set @EipSubMumber= '0 ' + @@IDENTITY
else
if @@IDENTITY> =1000
set @EipSubMumber=@@IDENTITY
end if
end if
end if
end if
------解决方案-------------------------这样也可以------------------
declare @EipSubMumber varchar(10)
declare @id int
set @id=@@identity
if @id <1000
@eipSubMumber=right( '0000 '+cast(@id as varchar),4)
else
@eipSubMumber=cast(@id as varchar)
------解决方案--------------------declare @EipSubMumber varchar(10)
set @EipSubMumber =
case
when @@identity> 0 and @@identity <10 then '000 ' + convert(varchar,@@identity)
when @@identity> = 10 and @@identity <100 then '00 ' + convert(varchar,@@identity)
when @@identity> = 100 and @@identity <1000 then '0 ' + convert(varchar,@@identity)
else convert(varchar,@@identity)
end
select @EipSubMumber