Count how many Sundays, Mondays, Tuesdays... in current month using MS SQL
In this article I am going to explain how to display how many Sundays, Mondays, Tuesdays etc in the current month. Now I will be explaining here how to do this. Please read and use it your application whenever it is required.
Hai Friends,
in this article,also i will thing which is useful to others.
/*STARTING DATE OF THE CURRENT MONTH*/
Note:just change the number what do you want
SELECT DATEADD(D,-27,GETDATE())
SELECT DATEADD(D,-DATEPART(D,GETDATE())+1,GETDATE())
/*LAST DATE OF THE CURRENT MONTH*/
SELECT DATEADD(D,-DATEPART(D,GETDATE()),DATEADD(M,1,GETDATE()))
Now Counting the days in current month
WITH CTE AS
(
SELECT DATEADD(D,-DATEPART(D,GETDATE())+1,GETDATE())[FIRST SUNDAY DATE],DATENAME(DW,DATEADD(D,-DATEPART(D,GETDATE())+1,GETDATE()))[DAY NAME]
UNION ALL
SELECT DATEADD(D,1,[FIRST SUNDAY DATE]),DATENAME(DW,DATEADD(D,1,[FIRST SUNDAY DATE]))FROM CTE WHERE [FIRST SUNDAY DATE]<=DATEADD(D,-DATEPART(D,GETDATE()),DATEADD(M,1,GETDATE()))-1
)
SELECT [DAY NAME],COUNT([DAY NAME])FROM CTE GROUP BY [DAY NAME]
Thank you For reading my articles here.
if you have any queries please contact me in below mail and say your valuable comments here also.
Thank you.
My Mail Address is rathin59117@gmail.com
Reference: Rathin813.blogspot.com