Hi Mehram,
Check this example. Now please take its reference and correct your code.
SQL
DECLARE @StartDate AS DATE
DECLARE @EndDate AS DATE
DECLARE @CurrentDate AS DATE
SET @StartDate = CONVERT(DATE,'01/01/2020',103)
SET @EndDate = CONVERT(DATE,'04/01/2021',103)
SET @CurrentDate = @StartDate
CREATE TABLE #Date ([Date] VARCHAR(20))
WHILE (@CurrentDate < @EndDate)
BEGIN
INSERT INTO #Date VALUES
(CONVERT(CHAR(3), @CurrentDate, 0) + '-' + CONVERT(CHAR(4),RIGHT(DATEPART(YEAR,@CurrentDate),2)))
SET @CurrentDate = DATEADD(MONTH, 1, @CurrentDate)
END
SELECT * FROM #Date
DROP TABLE #Date
Output
Date |
Jan-20 |
Feb-20 |
Mar-20 |
Apr-20 |
May-20 |
Jun-20 |
Jul-20 |
Aug-20 |
Sep-20 |
Oct-20 |
Nov-20 |
Dec-20 |
Jan-21 |