Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. :)
Posted

 
Share this answer
 
Hi,

To add days from dates in SQL Server, have a look at the DATEDIFF function.

Which can be used like:
SQL
SELECT
  dateColumn,
  DATEADD(day,-7,dateColumn)
FROM
  yourTable
  ...


UPDATE:
In your query, I would use it something like this:
SQL
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.
 
Share this answer
 
v2
Comments
XCode2010 11-Sep-14 7:09am    
Man this is my sql statement. How can I make that through this.

<pre lang="SQL">

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 '9/1/2010' AND '9/15/2010')
</pre>
hypermellow 11-Sep-14 7:35am    
Have updated my solution to show you how to use the DATEDIFF function in your query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900