0

I have expiry and manufacturing dates (data type DATE) in products table in database and I want to apply following formula to get my desired result:

SELECT * 
  FROM products 
 WHERE (RecentDate - expiry / manufacturing - expiry) * 100 > = 70;

it will show the products whose life time has exceeded more than 70 percent.

How can I design a query for this task (I am using Derby ).

I tried this but didn't succeed

SELECT * FROM PRODUCTS
 WHERE ({fn TIMESTAMPDIFF(SQL_TSI_DAY, CURRENT_TIMESTAMP, EXPIRY)} / 
        {fn TIMESTAMPDIFF(SQL_TSI_DAY, MDATE, EXPIRY )}) * 100 > 70;

division of these two timestampdiff give 0.I don't know why?

carlspring
  • 31,231
  • 29
  • 115
  • 197
Hasnain
  • 99
  • 1
  • 12

2 Answers2

0

The the below idea:

select * from  products 
where 
convert(float,DATEDIFF(dd,manufacturing,GETDATE()))/convert(float,DATEDIFF(dd,manufacturing,expiry))*100 > = 70;
Mohammed
  • 313
  • 1
  • 6
0

After hours of playing around i sorted out the query.Thanks to all who helped me through!
Here it is:

SELECT EXPIRY,MDATE FROM PRODUCTS WHERE cast( {fn TIMESTAMPDIFF( SQL_TSI_DAY,MDATE,CURRENT_TIMESTAMP)} as double) /cast( {fn TIMESTAMPDIFF( SQL_TSI_DAY,MDATE,EXPIRY )} as DOUBLE)*100 > 70 ;

Solution was to cast the TIMESTAMPDIFF as DOUBLE.With out casting, it could not calculate the percentage..why, that i also am not sure.But some how it worked for me.Any one with knowledge, please do share.Cheers!

Hasnain
  • 99
  • 1
  • 12