如何获取今天(从今天到月份除外)的月份至今?

时间:2014-05-15 03:18:15

标签: sql sql-server date sql-server-2008-r2

我需要在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

即可

1 个答案:

答案 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);