Мне нужно показать время, прошедшее с определенной даты в годах, месяцах и днях, с учетом високосных годов / месяцев . Это должно быть в пределах сохраненного продукта в Firebird 2.1.
Я борюсь со следующим кодом:
SELECT CASE
WHEN :CustomDateTime > CURRENT_DATE THEN 0
WHEN EXTRACT(YEARDAY FROM :CustomDateTime) > EXTRACT(YEARDAY FROM CURRENT_DATE) THEN DATEDIFF(YEAR, :CustomDateTime, CURRENT_DATE) - 1
ELSE DATEDIFF(YEAR, :CustomDateTime, CURRENT_DATE)
END
FROM RDB$DATABASE
INTO :YearDiff;
SELECT CASE
WHEN :CustomDateTime > CURRENT_DATE THEN 0
WHEN (EXTRACT(MONTH FROM :CustomDateTime) = EXTRACT(MONTH FROM CURRENT_DATE))
AND (EXTRACT(YEARDAY FROM :CustomDateTime) > EXTRACT(YEARDAY FROM CURRENT_DATE))
THEN DATEDIFF(MONTH, DATEADD(:YearDiff YEAR TO :CustomDateTime), CURRENT_DATE) - 1
ELSE DATEDIFF(MONTH, DATEADD(:YearDiff YEAR TO :CustomDateTime), CURRENT_DATE)
END
FROM RDB$DATABASE
INTO :MonthDiff;
SELECT CASE
WHEN :CustomDateTime > CURRENT_DATE THEN 0
WHEN (:YearDiff > 0) AND (:MonthDiff > 0) THEN DATEDIFF(DAY, DATEADD(:MonthDiff MONTH TO DATEADD(:YearDiff YEAR TO :CustomDateTime)), CURRENT_DATE)
WHEN (:YearDiff > 0) AND (:MonthDiff = 0) THEN DATEDIFF(DAY, DATEADD(:YearDiff YEAR TO :CustomDateTime), CURRENT_DATE)
WHEN (:YearDiff = 0) AND (:MonthDiff > 0) THEN DATEDIFF(DAY, DATEADD(:MonthDiff MONTH TO :CustomDateTime), CURRENT_DATE)
ELSE DATEDIFF(DAY, :CustomDateTime, CURRENT_DATE)
END
FROM RDB$DATABASE
INTO :DayDiff;
Есть идеи как это исправить?