sql - Issue calculating month end date in 4GL -
this routine returning 12/31/2016 instead of 12/31/2015 , messing report. idea going wrong?
let date_month = month(p_selection.date_from) if date_month = 12 let date_month = 1 let p_selection.date_from = p_selection.date_from + 1 units year let date_thru = date_month,"/01/",year(p_selection.date_from) let p_selection.date_from = p_selection.date_from - 1 units year else let date_month = date_month + 1 let date_thru = date_month,"/01/",year(p_selection.date_from) end if let p_selection.date_thru = date_thru clipped if year(p_selection.date_thru) <> year(p_selection.date_from) let p_selection.date_thru = p_selection.date_thru + 1 units year end if let p_selection.date_thru = p_selection.date_thru - 1
assuming input p_selection.date_from
12/01/2015 ...
if date_month = 12
returns true, date_thru
gets calculated 01/01/2016
but second if
statement returns true, adding year p_selection.date_thru
(01/01/2017), before decrementing 1 day 12/31/2016.
seems me has had several goes @ trying calculate date of last day of month, , 1 or other method required, not both. 1 appears first in code particularly dubious - there's assumption dbdate
format when casts between date , char, entirely unnecessary.
a far simpler solution calculate:
let p_selection.date_thru = mdy(month(p_selection.date_from), 1, year(p_selection.date_from)) + 1 units month - 1 units day
in other words find first day of selected month, add month , subtract day. simple , robust, works @ year boundary , when leap day occurs.
Comments
Post a Comment