Community discussion forum

Help ! Sql query to get max value

  • 1 year ago

     I have a table (named "convDt") with a column (named "engdate") that stores date
            which has the values 1/1/2002, 1/1/2003, 1/2/2004 ,...,2/4/2010, 3/5/2015,.........

            Now i need to write a query that returns only the maximum year.
            Please some one help me.

  • 1 year ago

    Hi,

    You can use the datepart function to look at just the year, to retrieve the maximum year you can just order the records in descending order of year, the first record is the maximum year, try something like this:

    SELECT TOP 1 DATEPART(YEAR, engdate) AS theDate
    FROM convDt
    ORDER BY theDate Desc

  • 1 year ago

    Just in case I misunderstood, the following query will return all of the records from the maximum year:

    SELECT * FROM convDt WHERE DATEPART(YEAR, engdate) IN(
    SELECT TOP 1 DATEPART(YEAR, engdate) AS theDate
    FROM convDt
    ORDER BY theDate Desc)

  • 1 year ago

    Thanks sync_or_swimfor the help .

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback