This suffers from the same problem as my first attempt, it doesn't handle
da_first = '2005-12-30'
da_last = '2006-01-02'
I came up with this
Code:
SELECT (
(MONTH(da_end) - MONTH(da_start)) +
(12 * (YEAR(da_end) - YEAR(da_start))) +
(CASE WHEN DAY(da_end) > DAY(da_start) THEN 1 else 0 END)
) as nbr_months
FROM (SELECT cast('2006-02-28' as date) as da_start,
cast('2006-03-30' as date) as da_end
FROM "SYSIBM"."SYSDUMMY1"
) as T
;
it handles da_start = '2005-12-30' and da_end = '2006-01-02' correct
but it can't handle the current example: it should return 1, but it returns 2