Вопрос

I need to find month-to-yesterday in SQL Server 2008 R2.

I have:

SELECT COALESCE(Sum(subtotal), 0) 
FROM   dbo.dr_trans 
WHERE  transdate >= Dateadd(d, 0, Datediff(dd,0,Dateadd(dd,-(Day(Getdate()) 
                    - 1 ), Getdate()))) 

This gives me month to date BUT includes today. Is it as simple as changing the -1 to -2?

Это было полезно?

Решение

I would do this condition this way:

SELECT COALESCE(SUM(SUBTOTAL),0)
FROM dbo.DR_TRANS
WHERE TRANSDATE >= CAST(GETDATE() - DAY(GETDATE()) + 1 AS DATE) AND
      TRANSDATE < CAST(GETDATE() AS DATE);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top