473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 ***
Mar 15 '07 #1
2 6055
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 Developersdexhttp://www.developersdex.com***
select max(datepart(yy, myColumn)) maxYear,
min(datepart(yy, myColumn)) minYear
from myTable

Mar 15 '07 #2
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.

Mar 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pete | last post by:
I have a combobox which is used to select records, which is satisfactory at the moment. However, a second user is going to start using this database and there will be 1600 records. This makes...
7
by: sparkle | last post by:
Hi Everybody, I'm filling a combobox from a class, which works fine on it's own. But when I insert code to fill in other controls something in the combobox fill is causing the...
2
by: Jerry LeVan | last post by:
Hi, Is there any generic to select columns by position ie select the first, third, and fifth columns from .... I can't see anything in SQL...not to sure about PL/PgSQL... Thanks
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
3
by: sp | last post by:
i have an xml file <catalog> <cd> <title>Romanza</title> <artist>Andrea Bocelli</artist> <country>EU</country> <company>Polydor</company> <price>10.80</price> <year>1996</year>
4
by: GregginOttawa | last post by:
I've been Googling and reading for 5 days now trying to figure out how to write a query that selects records with dates in the current week (Sun to Sat). You'd think it would be remarkably simple...
1
by: vunderusaf | last post by:
I have a listbox on a form that is selecting using named FinalQuery: SELECT ., ., . FROM FinalQuery; Now I have a text field with a date on this form and I'd like to use that date as the...
4
by: tone | last post by:
Hi, need some help. Got table A with two columns: type DATE, VALUE Got table B with two columns: type DATE, VALUE Now the thing is to update table A with the values from table B. For each...
1
by: krishnadrk | last post by:
ReceiptDesc> <Receipt> po_nbr>123</po_nbr> ReceiptDtl> <receipt_date>
1
by: KingofGing | last post by:
Hi All, I'm having a problem writing an SQL statement that I can't quite wrap my head around. First, the background: I have a journal subscription system including 3 tables, tblSubscription,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.