Hi Waghmare,
Refer below sample query.
SQL
CREATE TABLE #Sale_Details(Id INT,lastauditdate DATETIME)
INSERT INTO #Sale_Details VALUES(1,GETDATE())
DECLARE @Date DATETIME
SET @Date = GETDATE()-5
CREATE TABLE #TemTable(Id INT, lastauditdate DATETIME)
IF(GETDATE()- 5 < (SELECT TOP 1 lastauditdate FROM #Sale_Details ORDER BY Id DESC))
BEGIN
INSERT INTO #TemTable(Id, lastauditdate)
SELECT TOP 1 Id,lastauditdate FROM #Sale_Details ORDER BY Id DESC
END
SELECT * FROM #TemTable
DROP TABLE #TemTable
DROP TABLE #Sale_Details