Avg DATEDIFF in Pervasive SQL query

I need to find the avg DATEDIFF for a group in Pervasive SQL 9

The following query will return the datediff, but when I try to place a AVG() around the convert, I get an Error in expression.

///This works

select  

convert(datediff(day, convert(

left(convert("date of stock",sql_char),4)+'-'+

right(left(convert("date of stock",sql_char),6),2)+'-'+

right(convert("date of stock",sql_char),2)

,sql_date),convert(

left(convert("disposal date",sql_char),4)+'-'+

right(left(convert("disposal date",sql_char),6),2)+'-'+

right(convert("disposal date",sql_char),2)

,sql_date)),sql_integer) as mydatediff
from "Inventory"
where left("disposal date",6) = 201002
and "disposal date" > 0
and
convert(datediff(day, convert(

left(convert("date of stock",sql_char),4)+'-'+

right(left(convert("date of stock",sql_char),6),2)+'-'+

right(convert("date of stock",sql_char),2)

,sql_date),convert(

left(convert("disposal date",sql_char),4)+'-'+

right(left(convert("disposal date",sql_char),6),2)+'-'+

right(convert("disposal date",sql_char),2)

,sql_date)),sql_integer) <> 0


////This does not....returns [LNA][Pervasive][ODBC Engine Interface]Expression evaluation error.

select  

AVG(convert(datediff(day, convert(

left(convert("date of stock",sql_char),4)+'-'+

right(left(convert("date of stock",sql_char),6),2)+'-'+

right(convert("date of stock",sql_char),2)

,sql_date),convert(

left(convert("disposal date",sql_char),4)+'-'+

right(left(convert("disposal date",sql_char),6),2)+'-'+

right(convert("disposal date",sql_char),2)

,sql_date)),sql_integer)) as mydatediff
from "Inventory"
where left("disposal date",6) = 201002
and "disposal date" > 0
and
convert(datediff(day, convert(

left(convert("date of stock",sql_char),4)+'-'+

right(left(convert("date of stock",sql_char),6),2)+'-'+

right(convert("date of stock",sql_char),2)

,sql_date),convert(

left(convert("disposal date",sql_char),4)+'-'+

right(left(convert("disposal date",sql_char),6),2)+'-'+

right(convert("disposal date",sql_char),2)

,sql_date)),sql_integer) <> 0

griffawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mirtheilSoftware DeveloperCommented:
Issuing the following simple query doesn't return any errors:
select avg(datediff(day, '2010-01-01', '2010-03-28'))

You might want to make sure that the expression is correct.  When you say it works without the AVG function, what does it return?  

I'm using PSQL 10.30.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
griffawAuthor Commented:
mirtheil:

You were right as always! The error was not in the avg datediff. I needed to check both fields for >0.

Here is the finished query. Thanks!

select
"store_number",
avg(datediff(month, convert(left(convert("date of stock",sql_char),4)+ '-' +right(left(convert("date of stock",sql_char),6),2)+'-'+ right(convert("date of stock",sql_char),2) ,sql_date),convert(left(convert("disposal date",sql_char),4)+'-'+ right(left(convert("disposal date",sql_char),6),2)+'-'+ right(convert("disposal date",sql_char),2) ,sql_date)))
from "inventory"
where left("disposal date",6) = 201002
and "disposal date" > 0
and "date of stock" > 0

group by "store_number"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.