I have two dates and I need to check that these two dates are within the same month of the same year.
I have the following solution
select to_char(:date1::date, 'yyyymm')::int2 = to_char(:date2::date, 'yyyymm')::int2
who has a better solution?
I have two dates and I need to check that these two dates are within the same month of the same year.
I have the following solution
select to_char(:date1::date, 'yyyymm')::int2 = to_char(:date2::date, 'yyyymm')::int2
who has a better solution?
2
Answers
how about using
date_trunc()
which compares month year:and if you only need same month regardless ofyear you can use
date_part
:There are different ways to do this. Below are few of them depending on does the month should be within the same year or not.
1. same month – same year
2. same month – any year
See the fiddle here.