Unix Technical Forum

selecting max and min year

This is a discussion on selecting max and min year within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a table that has a DateTime column which uses a DataTime datatype. How do I retrieve the ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 01:42 PM
Eugene Anthony
 
Posts: n/a
Default 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 ***
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 01:42 PM
ZeldorBlat
 
Posts: n/a
Default Re: selecting max and min year

On Mar 15, 2:47 pm, Eugene Anthony <solomon_13...@yahoo.com> wrote:
> 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 01:42 PM
--CELKO--
 
Posts: n/a
Default 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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:39 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com