Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL: Get average time between 2 dates

Posted on 2013-11-16
3
Medium Priority
?
723 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

730 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