Based on the information you have provided, I assume you want to get values from the list that are the specified period either side of your "special time".
Here's one way to do it using DATEADD
:
-- temp table for your sample data
CREATE TABLE #times ( val TIME )
INSERT INTO #times
( val )
VALUES ( '12:45:24' ),
( '13:05:00' ),
( '13:50:30' ),
( '14:50:32' ),
( '15:15:10' )
DECLARE @special_time TIME = '13:25:00'
DECLARE @diff_value TIME = '00:20:00'
-- variable will hold the total number of seconds for your interval
DECLARE @diff_in_seconds INT
-- gets the total number of seconds of your interval -> @diff_value
SELECT @diff_in_seconds = DATEPART(SECOND, @diff_value) + 60
* DATEPART(MINUTE, @diff_value) + 3600 * DATEPART(HOUR, @diff_value)
-- get the values that match the criteria
SELECT *
FROM #times
WHERE val = DATEADD(SECOND, @diff_in_seconds, @special_time)
OR val = DATEADD(SECOND, -( @diff_in_seconds ), @special_time)
DROP TABLE #times
Note that the WHERE
clause filters the results by adding and subtracting the difference. The subtraction is achieved by making the @diff_in_seconds
negative.