This routine is returning 12/31/2016 instead of 12/31/2015 and messing up a report. Any idea where it is going wrong?
LET date_month = MONTH(p_selection.date_from)
IF date_month = 12 THEN
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) THEN
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 the input
p_selection.date_fromis 12/01/2015 ...IF date_month = 12returns TRUE, sodate_thrugets calculated as 01/01/2016But then the second
IFstatement also returns TRUE, adding another year top_selection.date_thru(01/01/2017), before decrementing it one day to 12/31/2016.Seems to me that someone has had several goes at trying to calculate the date of the last day of the month, and one or the other method is required, not both. The one that appears first in your code is particularly dubious - there's an assumption that
DBDATEis US format when it casts between DATE and CHAR, which is entirely unnecessary.A far simpler solution would be to simply calculate:
In other words find the first day of the selected month, add a month and subtract a day. Simple and robust, it works at the year boundary and when a leap day occurs.