Codrut Rotaru Codrut Rotaru - 1 year ago 82
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?

Answer Source
SELECT TOP 1 * ........... order by ABS(datediff(second,table_timeStamp, @myTimestamp) ) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download