Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Nearest time from 3rd record

Author  Topic 

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-05-30 : 07:08:12
How can I get the nearest time record based on the 3rd record?

in MySQL I used like this

SELECT id, sampletime
FROM tablename
ORDER BY ABS(TIMESTAMPDIFF(SECOND, sampletime, '2007-05-23 16:40:00')) ASC
LIMIT 1

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-30 : 07:18:42

Select top 1 * from @T1
order by abs(datediff(s, tcol4, '2003-01-04 00:00:40.000'))

--------------------------------------------------
S.Ahamed
Go to Top of Page

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-05-30 : 07:21:03
thnx
Go to Top of Page

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-05-30 : 07:33:43
not jet..

by the following query

Select top 1 * from NF_DATA
order by abs(datediff(s, sampletime, '2007-02-21 12:25:09.000'))

I get the record where the time is '2007-02-21 12:25:09.000'
Go to Top of Page

Nowy
Yak Posting Veteran

57 Posts

Posted - 2007-05-30 : 07:42:11
Now it works :)

select max(SampleTime)
from NF_DATA
where SampleTime in (Select top 2 SampleTime from NF_DATA order by abs(datediff(s, sampletime, '2007-02-21 12:25:09.000')) )
Go to Top of Page
   

- Advertisement -