Too much caffeinated today:hehe:
DECLARE @FromDate DATETIME, @ToDate DATETIME
DECLARE @FromDateConverted INT, @ToDateConverted INT
SELECT @FromDate = '2009-04-09',
@ToDate = '2009-04-10',
@FromDateConverted = DATEDIFF( SECOND, '19700101', @FromDate ),
@ToDateConverted = DATEDIFF( SECOND, '19700101', @ToDate )
; WITH ConvertedTimes
AS
(
SELECT sl_no,
DATEADD( SECOND, Initial_date, '19700101' ) AS Initial_date,
DATEADD( SECOND, Log_Time, '19700101' ) AS Log_Time, [Description]
FROM TEST
-- Filter the records by the selected period
WHERE Initial_date >= @FromDateConverted AND Initial_date 0 THEN Log_Time ELSE NULL END ) AS Log_Time
FROM ConvertedTimes
)
SELECT sl_no, Initial_date, Log_Time, [Description]
FROM TestCTE
WHERE RowNumber = 1