how to get the max date in the table

  • I have a  below table with the values

    cscode      StartDate       EndDate

    0011    5/9/2022      10/1/2022

    0011    6/17/2022    1/1/2023

    0011    5/9/2022      10/1/2022

    The date column is varchar. I need to take the latest mm/dd/yyyy using EndDate. For ex, in the above table I need to take 01/01/2023. Also I need to select cscode and startdate. So my output looks like below

    cscode      StartDate       EndDate

    0011    6/17/2022    1/1/2023

    I tried the below query

    select max(convert(date, EndDate,101)) as EndDate, StartDate from table1 where cscode = '0011' group by StartDate

    I am getting the below output

    EndDate    StartDate

    10/1/2022     5/9/2022

    1/1/2023        6/17/2022

    I need only one result of the latest mm/dd/yyyy. Please correct me if i did anything wrong in the query.

  • Here is one way

    SELECT TOP (1) <column converted to date>, other columns
    FROM table
    ORDER BY <column converted to date> DESC

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

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

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