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)
)
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:
No comments:
Post a Comment