Codrut Rotaru Codrut Rotaru - 2 months ago 4x
SQL Question

Compare datetimes from SQL Server table with a datetime from user and get the date from table closest to the user's datetime

I have a table in SQL Server 2014 with time stamps.

This is my table:

enter image description here

I want to compare each time stamp from my table with a time stamp that I input and get from my table the time stamp for which the

datediff(table_timeStamp, @myTimestamp)
is the smallest. Hope it is clear what I want. This is for a function and I want to know how can I do that in the easiest way possible?

SELECT TOP 1 * ........... order by ABS(datediff(second,table_timeStamp, @myTimestamp) )