0

In the below image I am trying to start the week of "ManufactureDate" to every Monday but unable to get it the desired result.

Query used:

select distinct     dateadd(week, DATEDIFF(week, 0, [ManufactureDate]),0 )sp, [ManufactureDate] 
FROM ab

Current result Set:

SP                           Manufacturing Date
2019-10-14 00:00:00.000  2019-10-13 00:00:00.000
2019-10-14 00:00:00.000  2019-10-14 00:00:00.000
2019-10-14 00:00:00.000  2019-10-15 00:00:00.000
2019-10-14 00:00:00.000  2019-10-16 00:00:00.000
2019-10-14 00:00:00.000  2019-10-17 00:00:00.000
2019-10-14 00:00:00.000  2019-10-18 00:00:00.000
2019-10-21 00:00:00.000  2019-10-20 00:00:00.000

Desired result:

Sp        Manufacturing Date
10/14/2019  10/14/2019
10/14/2019  10/15/2019
10/14/2019  10/16/2019
10/14/2019  10/17/2019
10/14/2019  10/18/2019
10/14/2019  10/20/2019
10/21/2019  10/21/2019

Image:

enter image description here

Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
  • Did you actually need the image? Looks the same as the text data (which is better). – Dale K Nov 29 '19 at 02:17
  • @DaleK, no I didn't, I think I don't have that privilege to remove answers. – Ed Bangga Nov 29 '19 at 02:30
  • Possible duplicate of [Get the week start date and week end date from week number in SQL Server](https://stackoverflow.com/questions/1267126/get-the-week-start-date-and-week-end-date-from-week-number-in-sql-server) – Dale K Nov 29 '19 at 03:36

1 Answers1

4
SELECT 
DATEADD(DAY, ((DATEPART(WEEKDAY,[ManufactureDate]) + 5) % 7) * -1, [ManufactureDate]) Sp, [ManufactureDate]
FROM ab

Here's a fiddle.

Sp                  ManufactureDate
2019-10-07          2019-10-13
2019-10-14          2019-10-14
2019-10-14          2019-10-15
2019-10-14          2019-10-16
2019-10-14          2019-10-17
2019-10-14          2019-10-18
2019-10-14          2019-10-19
2019-10-14          2019-10-20
2019-10-21          2019-10-21
Max Szczurek
  • 4,324
  • 2
  • 20
  • 32