嗨,有一个temp #Dates table
使用以下条件填充的。
IF @Period = 'Monthly'
BEGIN
INSERT #Dates
SELECT MAX(DATE) WeekDays FROM TABLENAV WITH (NOLOCK, READUNCOMMITTED)
GROUP BY DATEDIFF(MONTH, 0, DATE)
ORDER BY MAX(DATE) DESC
END
现在数据在#Dates 临时表中
DATE
2013-04-22 00:00:00.000
2013-03-29 00:00:00.000
2013-02-28 00:00:00.000
2013-01-31 00:00:00.000
2012-12-31 00:00:00.000
2012-11-30 00:00:00.000
我想得到Previous_Date
当我通过任何Todays_Date
我试过如下
DECLARE @Todays_Date datetime = '2013-04-22'
select date from #Dates where MONTH(DATE) = MONTH(@Todays_Date)-1 AND YEAR(DATE) = YEAR(@Todays_Date)
giving ans : 2013-03-29
但是当年份改变时,我什么也得不到
例如,如果@Todays_Date = '2012-01-31'
它应该返回我2012-12-31
但什么都不给。
帮我修改我的select statement
.