dbTalk Databases Forums  

selecting max and min year

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss selecting max and min year in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Eugene Anthony
 
Posts: n/a

Default selecting max and min year - 03-15-2007 , 12:47 PM






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 ***

Reply With Quote
  #2  
Old   
ZeldorBlat
 
Posts: n/a

Default Re: selecting max and min year - 03-15-2007 , 01:47 PM






On Mar 15, 2:47 pm, Eugene Anthony <solomon_13... (AT) yahoo (DOT) com> wrote:
Quote:
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 Developersdexhttp://www.developersdex.com***
select max(datepart(yy, myColumn)) maxYear,
min(datepart(yy, myColumn)) minYear
from myTable



Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: selecting max and min year - 03-15-2007 , 04:41 PM



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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.