0

I'm trying to create a SQLite query that will check a column of dates in a table and select only the most recent one. The date format in table is dd/mm/yyyy

SELECT MAX(created) FROM account

That only seems to bring up the date with the highest day number.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adrian38
  • 55
  • 14
  • 2
    Change the format of your dates to YYYY-MM-DD and your code will work. – forpas Apr 09 '21 at 14:54
  • If you don't want to or can't convert the date field for some reason, you can cast a query as a date. Almost exact same question [has been asked before](https://stackoverflow.com/questions/4428795/sqlite-convert-string-to-date) – MyICQ Apr 09 '21 at 17:52
  • I'll have a go tomorrow with a clear head thanks – adrian38 Apr 09 '21 at 18:41
  • @forpas hey sorry for delayed reply, that worked great thanks for that – adrian38 Apr 12 '21 at 09:19

1 Answers1

0

Thanks to forpas for the response. This code worked with the suggested edit of changing the dates format in the table to yyyy-MM-dd:

cmd.CommandText = @"SELECT * FROM account AS a WHERE created = (SELECT MAX(created) FROM account AS b WHERE custid = @custid)";
                cmd.Parameters.AddWithValue("custid", Global.selectedCust);
                SQLiteDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    newestDate = read["created"].ToString();
                }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
adrian38
  • 55
  • 14