Katiankia
Katiankia

Reputation: 11

Select first, nearest date to today

I have a few dates in the YYYY-MM-DD format in the table column. how can I find the nearest date, less than or equal to today?

Normally I would use:

<= (SELECT TO_CHAR (TRUNC (SYSDATE), 'YYYY-MM-DD') from DUAL)

but it will return all values less or equal, and I only want one, the nearest.

Thanks for help.

Upvotes: 1

Views: 67

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

One method is:

select t.*
from (select t.*
      from t
      where datecol < sysdate  -- trunc(sysdate) ???
      order by datecol desc
     ) t
where rownum = 1;

Upvotes: 2

Related Questions