selecting max and min year

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eugene Anthony

    selecting max and min year

    I have a table that has a DateTime column which uses a DataTime
    datatype. How do I retrieve the minimum and maximum year using ms sql?

    Eugene Anthony

    *** Sent via Developersdex http://www.developersdex.com ***
  • ZeldorBlat

    #2
    Re: selecting max and min year

    On Mar 15, 2:47 pm, Eugene Anthony <solomon_13...@ yahoo.comwrote:
    I have a table that has a DateTime column which uses a DataTime
    datatype. How do I retrieve the minimum and maximum year using ms sql?
    >
    Eugene Anthony
    >
    *** Sent via Developersdexht tp://www.developersd ex.com***
    select max(datepart(yy , myColumn)) maxYear,
    min(datepart(yy , myColumn)) minYear
    from myTable

    Comment

    • --CELKO--

      #3
      Re: selecting max and min year

      SELECT DATEPART(yy, MAX (foo_date)) AS max_year,
      DATEPART(yy, MIN (foo_date)) AS min_year
      FROM Foobar;

      It is important not to put the DATEPART() inside the aggregate
      functions. It is easy to find the min and max on a column via indexes
      or statistics tables. Once you put a funciton inside an aggregate
      function, you cannot use these tools and have to do a table scan.

      Comment

      Working...