我需要在SQL Server 2008 R2中找到从月到昨。
我有:
SELECT COALESCE(Sum(subtotal), 0)
FROM dbo.dr_trans
WHERE transdate >= Dateadd(d, 0, Datediff(dd,0,Dateadd(dd,-(Day(Getdate())
- 1 ), Getdate())))
这给了我一个月到目前但包括今天。是否只需将-1
更改为-2
?
答案 0 :(得分:1)
我会这样做:
SELECT COALESCE(SUM(SUBTOTAL),0)
FROM dbo.DR_TRANS
WHERE TRANSDATE >= CAST(GETDATE() - DAY(GETDATE()) + 1 AS DATE) AND
TRANSDATE < CAST(GETDATE() AS DATE);