Week calculation from a specific period

后端 未结 5 1770
轻奢々
轻奢々 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:32

    Without the need of creating a table. Just replace the @date with your column date name

    declare @date datetime
    set     @date = '12/8/2016 00:00:00'
    
    select  case when datepart(month,@date) > 6 then 
    
                case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) > 52
                then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date) as nvarchar(4)),@date) / 7.0) end
    
            else
                case when CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) > 52
                then 52 else CEILING(datediff(day,'6/30/' + cast(datepart(year,@date)-1 as nvarchar(4)),@date) / 7.0) end
            end WorkWeek
    

    Compute Week number based on Fiscal Year

提交回复
热议问题