Get the week start date and week end date from week number in SQL Server

Get the week start date and week end date from week number in SQL Server

Asked on December 22, 2018 in SQL Server.
Add Comment


  • 5 Answer(s)

    By using this code one can find the day of week and add  date on days to get the start and end dates.

    DATEADD(dd, -(DATEPART(dw, WeddingDate)-1), WeddingDate) [WeekStart]
     
    DATEADD(dd, 7-(DATEPART(dw, WeddingDate)), WeddingDate) [WeekEnd]
    
    Answered on December 22, 2018.
    Add Comment

    This is the DATEFIRST simple solution:

    SET DATEFIRST 4 /* or use any other weird value to test it */
    DECLARE @d DATETIME
     
    SET @d = GETDATE()
     
      SELECT
      @d ThatDate,
      DATEADD(dd, 0 - (@@DATEFIRST + 5 + DATEPART(dw, @d)) % 7, @d) Monday,
    
    Answered on December 22, 2018.
    Add Comment

    Simply try this one line solution,

    SELECT DATEADD(day, DATEDIFF(day, 0, WeddingDate) /7*7, 0) AS weekstart,
           DATEADD(day, DATEDIFF(day, 6, WeddingDate-1) /7*7 + 7, 6) AS WeekEnd
    
    Answered on December 22, 2018.
    Add Comment
    Select 
          Sum(NumberOfBrides) As [Wedding Count], 
          DATEPART( wk, WeddingDate) as [Week Number],
          DATEPART( year, WeddingDate) as [Year],
          ??? as WeekStart,
          ??? as WeekEnd
    
        FROM  MemberWeddingDates
        Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate)
        Order By Sum(NumberOfBrides) Desc
    Answered on January 13, 2019.
    Add Comment
    SELECT DATEADD(day, DATEDIFF(day, 0, WeddingDate) /7*7, 0) AS weekstart,
             DATEADD(day, DATEDIFF(day, 6, WeddingDate-1) /7*7 + 7, 6) AS WeekEnd
    Answered on January 14, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.