C#

list all months between two dates




CREATE Function UDF_GetMonthRange(@FromDate date,@ToDate date)  
RETURNS table  
RETURN  
(  
SELECT  DATEADD(MONTH, x.number, @FromDate) AS Months    
FROM    master.dbo.spt_values x    
WHERE   x.type = 'P'            
AND     x.number <= DATEDIFF(MONTH, @FromDate, @ToDate)    
)  


get months range between two dates

Example:

DECLARE @FromDate date
DECLARE @ToDate date
SET @FromDate='2019-01-01'
SET @ToDate= '2020-07-15'
select * from dbo.UDF_GetMonthRange(@FromDate,@ToDate)

output:

get months range between two dates output

No comments:

Post a Comment