?
Solved

Doing subtraction on dates

Posted on 2004-08-20
2
Medium Priority
?
983 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 to return specific rows and columns, with various degrees of sorting and limits in place.

719 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