NoMan Ilyas
NoMan Ilyas

Reputation: 33

Convert string to Date then minus some months

I'm using Postgres with Rails. I've two string fields which store date like this, field_1 = "2020.06.09", I 'm comparing them with TO_DATE function and they are working finr but now I want to compare them after adding 5 months in one field. What I've tried so far is:

select * from users as u where TO_DATE(u.field_1, 'YYYY MM DD') > DATEADD(MONTH, 5, TO_DATE(u.field_2, 'YYYY MM DD'))

can anyone help me on this?

Thanks in advance.

Upvotes: 0

Views: 405

Answers (1)

user330315
user330315

Reputation:

There is no dateadd() in Postgres (or standard SQL), you need to add an interval:

select *
from users u
where TO_DATE(u.field_1, 'YYYY MM DD') > to_date(u.field_2, 'yyyy mm dd') + interval '5 month';

Upvotes: 1

Related Questions