Solved

SQL: Get average time between 2 dates

Posted on 2013-11-16
3
706 Views
Last Modified: 2013-11-17
I have a SQL Statement that lists CrateDate and PaymentDate for all users:  

SELECT     CONVERT(Datetime, tblUserProfileData.PaymentDate, 103) AS Datetime, aspnet_Membership.CreateDate
FROM         tblUserProfileData INNER JOIN
                      aspnet_Membership ON tblUserProfileData.UserId = aspnet_Membership.UserId
WHERE     (tblUserProfileData.PaymentDate <> '') AND (aspnet_Membership.CreateDate > CONVERT(Datetime, '31.01.2009', 103)) AND 
                      (aspnet_Membership.ApplicationId = '189bdd21-fc7a-123d-8a12-1706b1d6a329')

Open in new window


Now I need to find out how many days there is between CreateDate and PaymentDate for an average user. Example: The SQL returns "10" if the average between CreateDate and PaymentDate is 10 days.

Hope someone please can help me solving this :)
0
Comment
Question by:webressurs
[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
3 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39653382
Can you post some sample result of your query and the expected result?
Are you looking for datediff between CreateDate and PaymentDate?
You have used Datetime as column alias. One suggestion is not to use keywords as aliases. If you really want, you need to use it as [Datetime].
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39653413
SELECT  daysAverage = AVG(DATEDIFF(dd,b.CreateDate,a.PaymentDate))
FROM    tblUserProfileData  a
JOIN    aspnet_Membership   b  ON a.UserId = b.UserId
WHERE   a.PaymentDate <> ''
AND     b.CreateDate > CONVERT(Datetime, '31.01.2009', 103)

Open in new window

0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 39654381
Worked perfect, thank you :)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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