How to select a Record having closer Date

M

Mota

Hi;
I have a table having a non-unique field named dID and another field named
ExpiryDate.Assuming 5 records have a same dID but a different ExpiryDate,how
to select the record having nearest ExpiryDate from this 5 Records,using SQL
?
Thank you in advance.
 
M

Mike S.

SELECT Top 1 * FROM MyTable WHERE dID=12345 ORDER BY ExpiryDate

or ORDER BY ExpiryDate DESC depending on how you defined Expiration date.
 
M

Mota

Thank you for ur help;
And as another question
if we want to know how many days(or months) we have,to reach to that
ExpiryDate,which function is more efficient and what u recommend?
Thanx again.
 
M

Mike S.

SELECT Top 1 *, DateDiff("d", ExpiryDate, Now()) As NumOfDays, DateDiff("m",
ExpiryDate, Now()) As NumOfMons
FROM MyTable WHERE dID=12345 ORDER BY ExpiryDate
 
M

Mota

Thank you so so much.

Mike S. said:
SELECT Top 1 *, DateDiff("d", ExpiryDate, Now()) As NumOfDays, DateDiff("m",
ExpiryDate, Now()) As NumOfMons
FROM MyTable WHERE dID=12345 ORDER BY ExpiryDate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top