declare @lastmonthStartday datetime
declare @lastmonthEndday datetime
declare @nowdate datetime
select @nowdate =
replace(convert(varchar(20),getdate(),102),'.','')
-- for debug
select @nowdate = '20041201'
declare @year char(4),@month char(2),@day char(2)
declare @nextmonth char(2)
declare @lastmonth char(2)
set @month = datepart(mm,@nowdate)
if @month = 1
begin
set @year = datepart(yy,@nowdate)
set @year = @year - 1
end
else set @year = datepart(yy,@nowdate)
select @lastmonth = @month - 1
if @lastmonth = 0 set @lastmonth = 12
select @nextmonth = @month + 1
set @day = datepart(dd,@nowdate)
if (select len(@month)) = 1 select @month = '0' + @month
if (select len(@lastmonth)) = 1 select @lastmonth = '0' +
@lastmonth
if (select len(@nextmonth)) = 1 select @nextmonth = '0' +
@nextmonth
select @lastmonthStartday = @year + @lastmonth + '01'
set @year = datepart(yy,@nowdate)
select @lastmonthEndday = @year + @month + '01'
select @lastmonthEndday = @lastmonthEndday - 1
select @lastmonthStartday
select @lastmonthEndday
|