Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
GordonCopestake
Starting Member
8 Posts |
Posted - 2012-03-19 : 08:39:39
|
Hi All,I have a table SalesOrders with :OrderIDItemIDQtyDueDateand I would like to produce a summary report of the orders that groups my orders by week. I have used the below code from an example to add a "FridayDate" to the table to group against but this seems to jump to the NEAREST Friday, not the following Friday. e.g. if an order is on for the 19th March the FridayDate is being shown as 16th March instead of 23rd March.Could someone point out where I have gone wrong please?select *,DATEPART(wk, SalesOrders.DueDate) AS WkNo , dateadd(week, datediff(day,'20120106',DueDate) / 7, '20120106') AS FridayDatefrom SalesOrderswhere I've used 20120106 as a known Friday.Thanks in advanceGordon |
|
GordonCopestake
Starting Member
8 Posts |
Posted - 2012-03-19 : 08:54:09
|
Ah! I have fixed my own problem by using:CAST(DATEADD(d, 6 - DATEPART(dw,DueDate), DueDate) AS date) AS FridayDatemuch nicer code too |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-19 : 09:00:22
|
cool...thanks for sharing------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|