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 2005 Forums
 Transact-SQL (2005)
 How do I find the closest date

Author  Topic 

programer
Posting Yak Master

221 Posts

Posted - 2010-10-25 : 04:32:34
Hi,

I have big problem.

I have a table in which is Date

I want find the nearest date

Date:
1.1.2010
5.1.2010
15.3.2010


My date is 03.01.2010. How to get value 1.1.2010
?
My date is 08.01.2010. I want to get value 5.1.2010

My date is 28.3.2010. I want to get value 15.3.2010

How does it reach?
Regards

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-25 : 04:43:00
Do you always want the nearest less date ?

Try:
Select max(MyDate) from yourtable where
Mydate < WhateverdateYou Pass

Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-10-25 : 04:46:54
No, I just want the case to date does not exist.
If the date does not exist, it must find the earliest date.

quote:
Originally posted by pk_bohra

Do you always want the nearest less date ?

Try:
Select max(MyDate) from yourtable where
Mydate < WhateverdateYou Pass



Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-10-25 : 05:30:26
Try:
Select max(MyDate) from yourtable where
Mydate <= WhateverdateYouPass
Go to Top of Page
   

- Advertisement -