Jonas Palačionis
Jonas Palačionis

Reputation: 4842

Getting day difference in 2 dates in postgres

I have a table which looks like this:

datestamp
2020-04-01
2020-04-02

I am trying to do two things:

  1. Convert datestamp to date format, because right now it's ABC
  2. Get difference between datestamp and the end of this year ( 2020-12-31 )

So that I would get a result like this:

datestamp      diff
2020-04-01     275
2020-04-02     274

What I've tried:

DATEDIFF(day, datestamp, '2020-12-31 00:00:00.0000000')

I get:

DATEDIFF(day, datestamp, '2020-12-31 00:00:00.0000000')

Where is my mistake?

Upvotes: 0

Views: 29

Answers (1)

user330315
user330315

Reputation:

Simply subtract the values:

select datestamp, date '2020-12-31' - datestamp as diff
from the_table;

This assumes that datestamp is a date column.

Upvotes: 2

Related Questions