2016-03-09 65 views
0

我有一個查詢,我手動更改日期爲我喜歡。我該如何動態調整當前月份到日期的數據?如何在MSSQL中獲取月份日期數據?

SELECT SUM(op.price * op.qty) 
FROM dbo.Orders o 
     INNER JOIN dbo.OrdersProduct op ON op.orderID = o.orderID 
WHERE o.ID IN (xx, xx, xx, xx) 
AND o.orderDate BETWEEN '3/1/2016' AND '3/10/16' 
+0

[TSQL檢索當前月/年的所有記錄(http://stackoverflow.com/questions/13727800/tsql-retrieve-all-記錄式電流 - 月 - 年) – lad2025

回答

0

這將讓本月至今:

SELECT SUM(op.price * op.qty) 
FROM dbo.Orders o 
     INNER JOIN dbo.OrdersProduct op ON op.orderID = o.orderID 
WHERE o.ID IN (xx, xx, xx, xx) 
     AND o.orderDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) 
         AND 
         DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0)) 
相關問題