SELECT Y ||' Years, '|| M ||' Months, and '|| D ||' Days'
FROM
(
SELECT
CASE WHEN datediff(year, join_date, current_date) <> datediff(day, join_date, current_date)/365
THEN datediff(year, join_date, current_date)-1
ELSE datediff(year, join_date, current_date)
END as Y,
CASE WHEN datediff(year, join_date, current_date) <> datediff(day, join_date, current_date)/365
AND datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)<0
THEN datediff(month, dateadd(year, datediff(year, join_date, current_date)-1, join_date), current_date)-1
WHEN datediff(year, join_date, current_date) <> datediff(day, join_date, current_date)/365
AND datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)>=0
THEN datediff(month, dateadd(year, datediff(year, join_date, current_date)-1, join_date), current_date)
WHEN datediff(year, join_date, current_date) = datediff(day, join_date, current_date)/365
AND datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)<0
THEN datediff(month, dateadd(year, datediff(year, join_date, current_date), join_date), current_date)-1
ELSE datediff(month, dateadd(year, datediff(year, join_date, current_date), join_date), current_date)
END as M,
CASE WHEN datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)<0
THEN datediff(day, dateadd(month, datediff(month, join_date, current_date)-1, join_date), current_date)
ELSE datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)
END as D
FROM employees
)
SELECT Y ||' Years, '|| M ||' Months, and '|| D ||' Days'
FROM
(
SELECT
datediff(day, join_date, current_date)/365 as Y,
CASE WHEN datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)<0
THEN datediff(month, dateadd(year, datediff(day, join_date, current_date)/365, join_date), current_date)-1
ELSE datediff(month, dateadd(year, datediff(day, join_date, current_date)/365, join_date), current_date)
END as M,
CASE WHEN datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)<0
THEN datediff(day, dateadd(month, datediff(month, join_date, current_date)-1, join_date), current_date)
ELSE datediff(day, dateadd(month, datediff(month, join_date, current_date), join_date), current_date)
END as D
FROM employees
)
YEARS=DATEDIFF(YEAR FROM :BEGIN_DATE TO :END_DATE);
MONTHS=DATEDIFF(MONTH FROM :BEGIN_DATE TO :END_DATE);
DAYS=DATEDIFF(DAY FROM :BEGIN_DATE TO :END_DATE);
相对绿色的 Firebird n00b 这里有一个公平的 PostgreSQL 背景,虽然专业的 GIS 工作主要集中在数据库设计和采购/清理/制作空间多源数据集(通常质量可疑),然后查询可映射的结果作为答案到特定的位置谜语。因此,工具箱在 Admin/Optimization/3rd Party Application Setup 等方面很轻。在接下来的几周内,您可能(错误地,将会)看到我提出的一些 Firebird 特定问题。提前致谢!对这个稍微老一点的 Firebird 问题进行了尝试,以感受一下日期处理和连接的新感觉。进行了比预期更多的调整和测试,我的涉猎可能不是最有效/最优雅的解决方案,但是,假设表员工中存在列join_date,这将起作用:
当很明显时,子查询对我有条件:a)年和月的计算不考虑整个日期;b) 无论事件的顺序如何,日期减法都会愉快地返回负值。
年/月/日 示例:datediff() 在检索 31.12.2015 和 1.1.2016 之间的天数、月数或年数时返回值 1。
负值示例:当我使用以下方法计算天数减去整个月包含的天数时,大约 69% 的测试日期值小于零:
如果可以接受将 365 天替换为一年,我们可以简化子查询。该技巧适用于我的测试数据,但可能会在更广泛的日期范围内引发陷阱。简化的查询如下所示:
如果我搞砸了,或者我使用了糟糕/过度劳累的逻辑,那就大喊大叫。希望它在某些时候对某人有帮助!留意新的 Firebird 问题(可能已经陷入与不会丢失的 JDBC 连接字符串的斗争中)。
谢谢你,再次问好! - 抢
我从来没有计划——匿名者