Hi nirmal90,
Refer the below query and check.
DECLARE @LastId INT, @LastDate DATETIME, @Month INT, @Date INT, @NewId INT, @ItemName CHAR(10)
SET @ItemName = 'GTYYRX588'
SET @LastId = (SELECT TOP 1 BillId FROM FiscalYearTable ORDER BY [Date] DESC)
SET @LastDate = (SELECT TOP 1 [Date] FROM FiscalYearTable ORDER BY [Date] DESC)
SET @Month = (SELECT DATEPART(MONTH,@LastDate))
SET @Date = (SELECT DATEPART(DAY,@LastDate))
IF(@Date = 31 AND @Month = 3)
BEGIN
SET @NewId = 1
INSERT INTO FiscalYearTable VALUES(@NewId,@ItemName,GETDATE())
END
ELSE
BEGIN
SET @NewId = @LastId + 1
INSERT INTO FiscalYearTable VALUES(@NewId,@ItemName,GETDATE())
END
SELECT * FROM FiscalYearTable