I have SQL table in SQL server 2008, and I want to get latest record which depends on its date.
E.g. Lets say I have records with some column and a Date column which contain the date of creation of record.
Lets sat the date column contains following dates. 22-Dec, 23-Dec, 24-Dec, 25,Dec, 26-Dec.
Now , I want to fetch the record which is less than 25 Dec, but I want the latest date record, If I write the query
select * from Table where CreateDate < '25-Dec-2012'
then It will return 3 records but I want the latest record from them i.e. 24 Dec record
How to do it ?
You should add
TOP 1to your query, and order it in reverse of its natural order to get your last record first. Assuming that the default order is byCreateDatein ascending order, theORDER BY CreateDate DESCshould do the trick: