简体   繁体   English

DB2查询中的DATEDIFF

[英]DATEDIFF in DB2 query

I have a query from mysql that has been running on a table I recently migrated to DB2. 我有一个来自mysql的查询,该查询已在最近迁移到DB2的表上运行。

The query now fails on DB2 due to the line below, saying that DATEDIFF cannot be found. 现在,由于下面的行,该查询在DB2上失败,表明找不到DATEDIFF。 I'm assuming only because this isn't a valid function on db2. 我只是因为这不是db2上的有效函数而假定。

Is there an equivalent to this on DB2 that will maintain performance as well as function? 在DB2上是否有与之等效的东西,可以保持性能和功能?

SELECT DISTINCT
    LEAST(180, DATEDIFF(curdate(), start_date)) as days
FROM table2
where expire_date > curdate()

I use the DAYS() function to convert the dates to numeric sequential numbers and then just subtract them, as in: 我使用DAYS()函数将日期转换为数字序号,然后减去它们,如下所示:

SELECT DISTINCT
    LEAST(180, DAYS(curdate()) - DAYS(start_date)) as days
FROM table2
where expire_date > curdate()

According to DB2's manual, DAYS() returns: "The result is 1 more than the number of days from January 1, 0001". 根据DB2的手册, DAYS()返回:“结果比0001年1月1日起的天数多1”。

On Db2 11.1 (for Linux, Unix and Windows) and above, this will work 在Db2 11.1(适用于Linux,Unix和Windows)及更高版本上,这将起作用

SELECT DISTINCT
    LEAST(180, DAYS_BETWEEN(current_date, start_date)) as days
FROM table2
where expire_date > current_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM