Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2012-07-09 : 19:26:01
|
I want to find the average number of days between two date columns but SQL throws up an error and tells me that I the 'Operand data type datetime is invalid for avg operator.' Here is the code I have as of now:set @LoDate = '06/01/2012'set @HiDate = '06/30/2012'select AVG(RCV.RCV_DATE - INV.PMT_DATE)from INVJOIN POon PO.PO_ID = INV.PO_IDJOIN RCVon RCV.PO_ID = PO.PO_IDwhere INV.INV_DATE >= '06/01/2012' andINV.INV_DATE <= '06/30/2012'group by RCV.RCV_DATE,INV.PMT_DATEI also tried select AVG(SUM(RCV.RCV_DATE - INV.PMT_DATE)) and I got the same error message about SUM. Is there a way to do this?ThanksSherriSLReidForum NewbieRenton, WA USA |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-07-09 : 19:39:14
|
The subtraction operator when used on DATATYPE yields a result that is also of DATETIME type. And, you cannot take the average of that. If you really want the average number of days, use the datediff function to find the difference between the two dates, and take the average of that:select AVG(DATEDIFF(day, INV.PMT_DATE, RCV.RCV_DATE)) Note that I have PMT_DATE as the second parameter and RCV_DATE as the third parameter. That may not be what you want - if not, simply swap them. |
 |
|
sherrireid
Yak Posting Veteran
58 Posts |
Posted - 2012-07-10 : 13:59:36
|
Sunitabeck! Thank you so much! That was exactly what I was looking for. I appreciate your help.RegardsSherri ReidSLReidForum NewbieRenton, WA USA |
 |
|
|
|
|