hi,Below is my queryWITH Months AS( SELECT UPPER(convert(varchar(3),datename(month, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0)))) month_name, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0) startdate, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N + 1, 0) enddate FROM (VALUES (6),(5),(4),(3),(2),(1)) x(N))select month_name from months
i want to pass the value to this query and based on the value the values should be constructed.for example : declare @val intif @val = 2 then below should be constructedWITH Months AS( SELECT UPPER(convert(varchar(3),datename(month, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0)))) month_name, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0) startdate, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N + 1, 0) enddate FROM (VALUES (2),(1)) x(N))select month_name from months
if @val = 3 then below should be constructedWITH Months AS( SELECT UPPER(convert(varchar(3),datename(month, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0)))) month_name, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0) startdate, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N + 1, 0) enddate FROM (VALUES (3),(2),(1)) x(N))select month_name from months
if @val = 4 then below should be constructedWITH Months AS( SELECT UPPER(convert(varchar(3),datename(month, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0)))) month_name, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0) startdate, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N + 1, 0) enddate FROM (VALUES (4),(3),(2),(1)) x(N))select month_name from months
if @val = 5 then below should be constructedWITH Months AS( SELECT UPPER(convert(varchar(3),datename(month, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0)))) month_name, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N, 0) startdate, DATEADD(MM, DATEDIFF( MM, 0, GETDATE()) - N + 1, 0) enddate FROM (VALUES (5),(4),(3),(2),(1)) x(N))select month_name from months
how to achieve this dynamic construction. please help me