moe February 2016

Start the first day of the week as Sunday in SQL

I want my first day of the week to be as Sunday but my issue with my code is that it starts always as Monday.

SELECT 
    UID,
    DATEADD(DD,CONVERT(INT, (DATEDIFF(DD, '1/1/1900', t.DT)/7)) * 7,'1/1/1900') [WeekBeginDate], 
    SUM(HOURS) AS TOTAL_HOURS 
FROM 
    myTable t 
WHERE 
    DT >= DATEADD(WEEK, -6, GetDate()) 
GROUP BY 
    UID, CONVERT(INT, DATEDIFF(DD, '1/1/1900', t.DT)/7)

Answers


t-clausen.dk February 2016

This should do the trick:

SELECT
  UID,  
  DATEADD(d, -1, DATEDIFF(d, -1, t.DT)/7 * 7) [WeekBeginDate] ,
  SUM(HOURS) AS TOTAL_HOURS 
FROM myTable t 
WHERE DT >= DATEADD(WEEK, -6, GetDate()) 
GROUP BY UID, DATEDIFF(d, -1, t.DT)/7

Post Status

Asked in February 2016
Viewed 2,719 times
Voted 13
Answered 1 times

Search




Leave an answer