Presumably add_months
expects a date as its first argument and returns a date. You don't have dates, you have YYYY-MM strings so you have two problems:
add_months
won't know what to do with a YYYY-MM string.
- BETWEEN won't know what to do with a date and a YYYY-MM string.
If you want to use add_months
then you'll have to give it a date and convert the date it gives you to one of your YYYY-MM strings with something like this:
to_char(add_months((cal_date || '-01')::date, -13), 'yyyy-mm')
Appending -01
to your strings should give you a string representation of the first of that month and you should be able to cast that to a date
with ::date
. Then a to_char
to convert the result of add_months
back to your YYYY-MM format.
Alternatively, since add_months
isn't really doing anything useful for you here, just use a PostgreSQL interval for the month adjustment:
to_char((cal_date || '-01')::date - interval '13 months', 'yyyy-mm')