Hi
I have Date & no of Yearsas a parameter. I want no of days month wise till Date + No Of Years
Thamks
Hi
I have Date & no of Yearsas a parameter. I want no of days month wise till Date + No Of Years
Thamks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Naimish MakwanaPosted Oct 10, 2023, 4:14 AM
Hello Ramco,
Try below query
Thanks
Naimish Makwana
Jayraj ChhayaPosted Jan 3, 2024, 5:48 AM
To calculate the number of days month-wise, we can use SQL queries to generate a list of months and their corresponding number of days. Here's an example of how you can achieve this:
In the above example, we assume you have a table called 'calendar' with a column 'date' of type DATE. Adjust the table and column names according to your database schema.
To use this query, replace
{no_of_years}with the desired number of years. The query will generate a list of month names and their corresponding number of days for the specified duration.Anandu G NathPosted Jan 3, 2024, 5:32 AM
Check this
DECLARE @StartDate DATE = '2023-01-01'; -- Replace with your start date
DECLARE @NumberOfYears INT = 3; -- Replace with the number of years you want to add
-- Create a CTE to generate months
;WITH MonthCTE AS (
SELECT @StartDate AS StartOfMonth, DAY(EOMONTH(@StartDate)) AS DaysInMonth
UNION ALL
SELECT DATEADD(MONTH, 1, StartOfMonth), DAY(EOMONTH(DATEADD(MONTH, 1, StartOfMonth)))
FROM MonthCTE
WHERE DATEADD(MONTH, 1, StartOfMonth) <= DATEADD(YEAR, @NumberOfYears, @StartDate)
)
-- Select data from the CTE
SELECT FORMAT(StartOfMonth, 'MMM yyyy') AS MonthYear, DaysInMonth
FROM MonthCTE
OPTION (MAXRECURSION 0); -- Specify the recursion level (0 means unlimited)
Uday DodiyaPosted Oct 10, 2023, 4:44 AM
Hi Ramco,
Try Below Query
Output :