Help ! Sql query to get max value

sql server India
  • 9 years 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.

  • 9 years 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

  • 9 years 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)

  • 9 years ago

    Thanks sync_or_swimfor the help .

     

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The generation of random numbers is too important to be left to chance.” - Robert R. Coveyou