Technical Article

GetWeekNo

,

This will return the weekno of the given date

CREATE PROCEDURE [dbo].[GetWeekNo]
@dt_Date as nvarchar(20)
AS
DECLARE @StartDay nvarchar(20)
DECLARE @NoOfDays int
DECLARE @WeekNo int 
Set @WeekNo = 0
if ISDATE (@dt_Date ) = 1
begin
Set @StartDay  =  cast(year(@dt_Date)as nvarchar) + '/01/01'
Set @NoOfDays = cast(DATEDIFF(day,@StartDay, @dt_Date)as float)
if @NoOfDays = 0
   Set @WeekNo =1
else
   begin
      Set @WeekNo = cast(@NoOfDays/7 as int)
      if (cast((cast(DATEDIFF(day,@StartDay, @dt_Date)as float) /7) as float) - cast(@NoOfDays/7 as int)) > 0 
 begin
   Set @WeekNo =  @WeekNo + 1   
 end 
      if DATEPART(dw, GETDATE()) < DATEPART(dw, @StartDay)
 begin
    Set @WeekNo =  @WeekNo + 1   
 end 
   end
end
Select @WeekNo AS WeekNo
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating