SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Closest date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

chriztoph
Posting Yak Master

Philippines
155 Posts

Posted - 05/28/2012 :  05:13:52  Show Profile  Reply with Quote
Please Help!!

I have a list of dates and I have a parameter date to compare with these dates and I want to get the future closest one if

Parameter date: 2011-02-15 00:00:00
List:
2010-11-03 00:00:00
2010-12-04 00:00:00
2011-01-04 00:00:00
2011-02-04 00:00:00
2011-03-07 00:00:00
2011-04-07 00:00:00
2011-05-08 00:00:00
2011-06-08 00:00:00
2011-07-09 00:00:00
2011-08-09 00:00:00

Result: 2011-03-07 00:00:00

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/28/2012 :  05:19:17  Show Profile  Reply with Quote

select top 1 *
from   yourtable
order by abs(datediff(day, datecol, parameter_date))



KH
Time is always against us

Go to Top of Page

Villanuev
Constraint Violating Yak Guru

309 Posts

Posted - 05/28/2012 :  05:23:25  Show Profile  Reply with Quote
Please try.

Declare @tran datetime
Set @tran='2011-02-15 00:00:00'

DECLARE @T TABLE (trandate datetime)
INSERT @T (trandate)
Values
('2010-11-03 00:00:00'),
('2010-12-04 00:00:00'),
('2011-01-04 00:00:00'),
('2011-02-04 00:00:00'),
('2011-03-07 00:00:00'),
('2011-04-07 00:00:00'),
('2011-05-08 00:00:00'),
('2011-06-08 00:00:00'),
('2011-07-09 00:00:00'),
('2011-08-09 00:00:00')


Select min(trandate) from @T
where trandate > @tran
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/28/2012 :  13:49:58  Show Profile  Reply with Quote
quote:
Originally posted by khtan


select top 1 *
from   yourtable
order by abs(datediff(day, datecol, parameter_date))



KH
Time is always against us




This will give even dates less than passed date if difference is close enough than future date which i dont think is what OP's looking at

I have a parameter date to compare with these dates and I want to get the future closest one

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

chriztoph
Posting Yak Master

Philippines
155 Posts

Posted - 05/28/2012 :  20:17:31  Show Profile  Reply with Quote
Thanks guys for the help..

maybe I'll use Villanuev's..

To khtan and visakh16, thanks for giving other option..I can use this in the other projects..
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 05/28/2012 :  21:26:45  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by khtan


select top 1 *
from   yourtable
order by abs(datediff(day, datecol, parameter_date))



KH
Time is always against us




This will give even dates less than passed date if difference is close enough than future date which i dont think is what OP's looking at

I have a parameter date to compare with these dates and I want to get the future closest one

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





thanks for pointing out. I missed that


KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

Posted - 05/29/2012 :  01:27:53  Show Profile  Reply with Quote
quote:
Originally posted by khtan

quote:
Originally posted by visakh16

quote:
Originally posted by khtan


select top 1 *
from   yourtable
order by abs(datediff(day, datecol, parameter_date))



KH
Time is always against us




This will give even dates less than passed date if difference is close enough than future date which i dont think is what OP's looking at

I have a parameter date to compare with these dates and I want to get the future closest one

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





thanks for pointing out. I missed that


KH
Time is always against us




No problem
you're wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000