如何从SQL语句中的datetime.now(" d")中减去day? [英] How to subtract day from datetime.now("d") in SQL Statement?
问题描述
我想现在减去一天-7一周和-30一个月我想跟踪所有日期包括在现在之前。
例如:
现在是9/11/2014。
如果我减去-7它会显示。
9/11/2014
9/10/2014
9/9/2014
9/8/2014
9/7/2014
9/6/2014
9/5/2014
我不知道我的SQL语句能写什么。
感谢您的帮助。 :)
I would like to subtract day now -7 for a week and -30 for a month and I want also to track all the dates include before the date now.
For example:
Now is 9/11/2014.
If I subtract -7 it will display.
9/11/2014
9/10/2014
9/9/2014
9/8/2014
9/7/2014
9/6/2014
9/5/2014
I dont know what can I write in my SQL statement.
Thanks for your help. :)
推荐答案
见这里:查找两个日期之间的所有天数 [ ^ ]
您好,
要添加从SQL Server中的日期开始,请查看 DATEDIFF 功能。
可以使用如下:
Hi,
To add days from dates in SQL Server, have a look at the DATEDIFF function.
Which can be used like:
SELECT
dateColumn,
DATEADD(day,-7,dateColumn)
FROM
yourTable
...
更新:
在您的查询中,我会使用类似这样的内容:
UPDATE:
In your query, I would use it something like this:
SET DATEFORMAT mdy;
DECLARE @StartDate DATETIME = '9/1/2010';
DECLARE @EndDate DATETIME = DATEADD(day, 14, @StartDate);
SELECT
Customer_Transaction.Order_Number,
Customer_Purchased.ProductID,
Customer_Purchased.productName,
Customer_Purchased.Branch,
Customer_Purchased.quantity,
Customer_Transaction.Total_Purchased,
Customer_Purchased.Date_Purchased
FROM
Customer_Purchased INNER JOIN
Customer_Transaction
ON Customer_Purchased.Item_Purchased_Number = Customer_Transaction.Item_Purchased_Number
WHERE
(Customer_Purchased.Branch = 'SM Dasmariñas')
AND
(Customer_Purchased.Date_Report BETWEEN @StartDate AND @EndDate)
哦,我没有测试,或者通过解析器运行上面的查询,所以它可能不会按原样工作,但应该给你一个开始。
希望它有所帮助。
Oh,I've not tested, or ran the above query through a parser, so it probably won't work 'as is', but should give you a start.
Hope it helps.
这篇关于如何从SQL语句中的datetime.now(" d")中减去day?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!