Solved

Doing subtraction on dates

Posted on 2004-08-20
2
978 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can > be used for a Text field 6 51
Passing Parameter to Stored Procedure 4 24
Create snapshot on MSSQL 2012 3 21
sqlserver get datetime field and create a string 5 18
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

832 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