Hi @Ramana Kopparapu You can use the datepart(wk,@EndDate) function, which will return the date in the week of the year. Using this function, I designed such a stored procedure, you can try it.
create or alter proc countofMondays
(
@EndDate date,
@DayName VARCHAR(10),
@NumberOfMondays int output
)
AS
if @DayName = 'Monday'
begin
set datefirst 1
select @NumberOfMondays = datepart(wk,@EndDate) - 1
end
if @DayName = 'Sunday'
begin
set datefirst 7
select @NumberOfMondays = datepart(wk,@EndDate)
end
go
Declare @NumberOfMondays int,
@EndDate date = '20230404',
@DayName VARCHAR(10) = 'Monday';
exec countofMondays @EndDate,@DayName,@NumberOfMondays output
select @NumberOfMondays;
Best regards, Percy Tang
If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.