Week calculation from a specific period

后端 未结 5 1795
轻奢々
轻奢々 2021-01-29 06:52

suppose the fiscal year is starting from July/1st to June/30th.

I have to calculate the week no. accordingly in SQL Server 2005.

Please Suggest!

Many Tha

5条回答
  •  逝去的感伤
    2021-01-29 07:05

    If week 1 of your financial year is always 1st July to 7th July...

    DECLARE
      @inputDate    DATETIME,
      @fYearStart   DATETIME,
      @weekNumber   INT
    SET
      @inputDate    = getDate()
    SET
      @fYearStart   = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
    SET
      @weekNumber   = DATEDIFF(day, @fYearStart, @inputDate) / 7 + 1
    


    If your finacial weeks are always Sunday to Saturday...

    DECLARE
      @inputDate    DATETIME,
      @fYearStart   DATETIME,
      @weekNumber   INT
    SET
      @inputDate    = getDate()
    SET
      @fYearStart   = DATEADD(year, DATEDIFF(year, '20000101', DATEADD(month, -7, @inputDate)), '20000701')
    SET
      @weekNumber   = DATEDIFF(WEEK, @fYearStart, @inputDate)
    


    One or other of these should be adaptable to your definition of week number.

提交回复
热议问题