1

I'm currently using PostgreSQL 8.4 and need to substract from now() date on day. What I tried is now() - 1 and it didn't work. Although that page said that we could use operator - to perform operations on date. More preciseli I recieved

ERROR:  operator does not exist: timestamp with time zone - integer

Any ideas?

6
  • Unrelated, but: 8.4 is really old and no longer maintained. You should plan an upgrade to 9.3 (or even 9.4) as soon as possible. Commented Dec 15, 2014 at 8:53
  • @a_horse_with_no_name You're absolutely right. But these are our production needs to use that version. We just don't have time to migrate to later versions. Commented Dec 15, 2014 at 9:02
  • @a_horse_with_no_name BTW, is such a migration gonna be painful? Commented Dec 15, 2014 at 9:03
  • Probably not that painful. The migration from 8.3 to 8.4 probably was more "painful" because of the stricter datatype checking in 8.4 But apart from the changed bytea wire encoding I don't recall any major differences (8.4 is just too long ago...) Commented Dec 15, 2014 at 9:05
  • You can subtract an integer from a DATE, but not from a TIMESTAMP. Check the manual, it's all there: postgresql.org/docs/8.4/interactive/functions-datetime.html Commented Dec 15, 2014 at 9:10

1 Answer 1

2

PostgreSQL can't use mathematical functions directly I think. But we can use date function such interval function in PostgreSQL will solve your problem.

Try below subtract eg:

select interval '2 day' - interval '1 day';
select now() - interval '1' ;

You can also refer to PostgreSQL 8.4 documentation.

http://www.postgresql.org/docs/8.4/static/functions-datetime.html

Sign up to request clarification or add additional context in comments.

1 Comment

It's not about using math directly, it's about types. When you subtract from a date you need to subtract an interval of some kind. a second, a minute, a day etc. OTherwise your explanation makes great sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.