Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - selecting the first record found before a given date

Tags:

sql

Say I have a table:

ID  DATE
1   2/1/12
2   3/1/12
3   1/1/12
4   4/1/12

How would I go about selecting the first date found when decrementing from a given date.

Example: Find the last entry before 4/1/12, by date. Return entry at SQL ID 2.

If this was added:

ID  DATE
5   3/2/12

Than the above example would return the entry at SQL ID 5.

How would I represent what I need in SQL?

like image 932
user1429195 Avatar asked Sep 15 '25 13:09

user1429195


1 Answers

Select top 1 ID, DATE
from table
where DATE < '4/1/12'
order by DATE DESC
like image 169
Gratzy Avatar answered Sep 17 '25 04:09

Gratzy