If you want to avoid converting an integer, in YYYYMM
format, to and from a date, you can just use maths and CASE
statements...
For example YYYYMM % 100
will give you MM
. Then you can check if it's 2 or less. If it is 2 or less, deduct 100 to reduce by a year, and add 12 to get the month as 13 or 14. Then, deducting 2 will give you the right answer.
Re-arranging that, you get YYYYMM - 2 + (88, if the month is 1 or 2)
sampledate <= YYYYMM - 2 + CASE WHEN YYYYMM % 100 <= 2 THEN 88 ELSE 0 END
The better idea may just be to reshape your data so that you actually have a (real) date field, and just use ADD_MONTHS(aRealDate, -2)
...
EDIT:
If your actual issue is generating the YYYYMM
value for "two months ago", then deduct the 2 months before you use the YEAR()
and MONTH()
functions.
year( ADD_MONTHS(from_unixtime(unix_timestamp()), -2) )*100
+
month( ADD_MONTHS(from_unixtime(unix_timestamp()), -2) )