views:

107

answers:

2

Hi,

I have a table in a sql server 2008 database that contains bunch of records as well as a date column. The date is inserted automatically when a new entry to the table occurs. So, it contains the date of the record that has been created.

I am trying to run a query that would return me the earliest date and the latest date in this table.

I tried something like;

SELECT     TOP(1) DateAdded AS firstdate FROM News ORDER BY DateAdded DESC;  SELECT TOP(1) DateAdded AS lastdate FROM News ORDER BY DateAdded ASC;

but it only returned the 'firstdate'.

Can anyone guide me on how to achieve this?

+3  A: 
SELECT 
       MIN(DateAdded) As FirstDate,
       MAX(DateAdded) As LastDate
FROM
       News;
Jose Basilio
+1  A: 

The answer is to use aggregates.

SELECT
    MIN(DateAdded) AS firstdate,
    MAX(DateAdded) AS lastdate
FROM
    News;

Your query returns 2 results: each works individually though

gbn
thank you for the answer, I marked Jose's answer as he wrote first and I dont want to offend anyone...
Emin
I can imagine a bunch of us racing to answer this one... Lucky Jose :-)
gbn