handling dates in derby within query

假装没事ソ 提交于 2019-12-10 11:55:01

问题


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?


回答1:


The the below idea:

select * from  products 
where 
convert(float,DATEDIFF(dd,manufacturing,GETDATE()))/convert(float,DATEDIFF(dd,manufacturing,expiry))*100 > = 70;



回答2:


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!



来源:https://stackoverflow.com/questions/27840182/handling-dates-in-derby-within-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!