日期:2014-05-17  浏览次数:20539 次

sql 求时间的函数
declare @fyear as int,@fmonth as int
set @fyear=2012
set @fmonth=9
求它上个月,上上个月,上上上个月的年月怎么求,例如 现在是2012 09 上个月就是2012 08 上上个月就是2012 07
上上上个月就是2012 06
如果 
set @fyear=2012
set @fmonth=2
上个月就是2012 01 上上个月就是2011 12 上上上个月就是2011 11

这个用sql怎么写呢

------解决方案--------------------
SQL code
declare @fyear as int,@fmonth as int
set @fyear=2012
set @fmonth=9

select convert(varchar(7),dateadd(mm,-1,ltrim(@fyear)+'-'+ltrim(@fmonth)+'-01'),120)

------解决方案--------------------
SQL code
declare @fyear as int,@fmonth as int
set @fyear=2012
set @fmonth=9
SELECT DATEADD(mm,-1,LTRIM(@fyear)+'-'+LTRIM(@fmonth)+'-01')
SELECT DATEADD(mm,-2,LTRIM(@fyear)+'-'+LTRIM(@fmonth)+'-01')