Max date

  • There is a column [OrderDate] store data as varchar format as 'MM/dd/yyyy'.
    For example, 
    04/02/2018
    02/15/2019
    12/31/2017
    How to code to select max date which is '02/15/2019' ?

  • convert to date
    select max converted date

    and fix your tables - dates should never  be stored as anything other than dates .

  • frederico is right, you should change the table definition to store date type values. But you can order that string in date order
    SELECT TOP(1) *
      FROM dbo.myTable
     ORDER BY RIGHT(OrderDate, 4) + SUBSTRING(OrderDate, 4, 2) + LEFT(OrderDate, 2) DESC

    or
    SELECT TOP(1) *
      FROM dbo.myTable
     ORDER BY convert(date,OrderDate,101) DESC

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply