Solved

Doing subtraction on dates

Posted on 2004-08-20
2
975 Views
Last Modified: 2012-05-05
I need to determine the average age for a set of data. I have the create date for each record and need to subtract that to get the age of each row. Then I also need to sum that up so that I have the total and can get the average. Is there a way to do this using SQL?

Thanks,
John
0
Comment
Question by:Pawlikj
2 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 11854676
Pawlikj:

> I need to determine the average age for a set of data. I have the create date for
> each record and need to subtract that to get the age of each row. Then I also need
> to sum that up so that I have the total and can get the average. Is there a way to
> do this using SQL?

Yes, you can use the DATEDIFF function to get the age of the record.  Say you want to get the age in terms of "Days", you could do this:
    SELECT *, DATEDIFF(days, CreateDate, GETDATE()) As DaysOld FROM YourTable

However, if you just want to find the average, then SQL can do all the work for you:
    SELECT AVG( DATEDIFF(days, CreateDate, GETDATE()) ) As AverageDaysOld FROM YourTable

Hope That Helps,
Dex*
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11854679
You can use DATEDIFF function to find out the difference between two dates.

For syntax, check books online or

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_da-db_5vxi.asp

Similarly, If you want to add the dates you can use DATADD function. But, not sure what is the requirement, looks like, for your query, it is a normal addition (not date additioin) to find the average.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now