Get latest date record - SQL query

2017/10/05 03:02
To return the latest record (one record only) from data base table where one of the fields is in Date/Time format:
SELECT TOP 1 * FROM table_name ORDER BY date_time_column_name DESC
  • for ORACLE instead of TOP 1,  ROWNUM shall be used
  • the query may return NULL value - suitable exception handling must be provided

Programming in C# using data binding and data view

DataView dv = new DataView(DataSet_name.table_name);
string sort = "Date_column_name DESC";
dv.Sort = sort;
DateTime rdt;
if (dv.Count > 0) 
    rdt = Convert.ToDateTime(dv[0]["Date_column_name"]);

//   Do not forget to fill the corresponding table adapters before creating data view.


// and only then

DataView dv = new DataView(DataSet_name.table_name);