Obtains the dataset closest to the specified time.
Source: Internet
Author: User
If object_id ('[tabletest]') is not null drop table [TBL] Create Table [tabletest] ([datime] datetime, [daweight] varchar (5 )) insert [tabletest] Select '2014-03-15 14:24:00. 000', '000000' Union allselect' 300Kg-03-15 14:25:00. 000 ', '000000' godeclare @ datimenow datetimeset @ datimenow = getdate () select * From tabletest -- directly obtain the latest record select top 1 * From tabletest order by datime desc -- obtain the latest record earlier than the current time select top 1 * from t Abletest where datime <@ datimenow order by datime desc -- if it is the closest to the current time, including greater than the current time, it is calculated by second. -- if there is only one record. select top 1 * From tabletest order by ABS (datediff (SS, datime, @ datimenow) -- if there may be multiple records. select * From tabletest Where ABS (datediff (SS, datime, @ datimenow) = (select Min (ABS (datediff (SS, datime, @ datimenow) from tabletest) -- sort by difference value and obtain the serial number. Returns data based on the specified serial number. Select * from (select row_number () over (order by datediff (MI, datime, getdate () as ID, * From tabletest) A where id = 1
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.