158

I have the following table projects.

id title        created_at               claim_window
1  Project One  2012-05-08 13:50:09.924  5
2  Project Two  2012-06-01 13:50:09.924  10

A) I want to find the deadline with the calculation deadline = created_at + claim_window, where claim_window is the number of days. Something like following:

id title        created_at               claim_window  deadline
1  Project One  2012-05-08 13:50:09.924  5             2012-05-13 13:50:09.924
2  Project Two  2012-06-01 13:50:09.924  10            2012-06-11 13:50:09.924

B) I also want to find the projects whose deadline is gone:

id title        created_at               claim_window  deadline
1  Project One  2012-05-08 13:50:09.924  5             2012-05-13 13:50:09.924

I tried something like following, but it didn't work.

SELECT * FROM "projects" 
WHERE (DATE_PART('day', now()- created_at) >= (claim_window+1))

4 Answers 4

234

This will give you the deadline :

select id,  
       title,
       created_at + interval '1' day * claim_window as deadline
from projects

Alternatively the function make_interval can be used:

select id,  
       title,
       created_at + make_interval(days => claim_window) as deadline
from projects

To get all projects where the deadline is over, use:

select *
from (
  select id, 
         created_at + interval '1' day * claim_window as deadline
  from projects
) t
where localtimestamp at time zone 'UTC' > deadline
Sign up to request clarification or add additional context in comments.

6 Comments

but one more issue is occur my created_at has datatype timestamp without time zone AND current_timestamp has datatype timestamp with time zone, hence i don't get correct answer
@Salil: LOCALTIMESTAMP is a timestamp without time zone
@Salil: It also does not matter, the result would be the same with either timestamp or timestamptz, as long as the data is referring to the same point in time. Adding a day has the same effect on both.
my create_at is get saved in the UTC time (W/o timezone) and LOCALTIMESTAMP & CURRENT_TIMESTAMP gives me time in the +5.30, hence probles is still not get solved
@Salil: then re-calculate the localtimestamp to utc using localtimestamp at time zone 'UTC'
|
36

For me I had to put the whole interval in single quotes not just the value of the interval.

select id,  
   title,
   created_at + interval '1 day' * claim_window as deadline from projects   

Instead of

select id,  
   title,
   created_at + interval '1' day * claim_window as deadline from projects   

Postgres Date/Time Functions

1 Comment

created_at + '1 day' worked for me as well.
12

you can just use the below code to append or substract any date field

select date('08/30/2021') + 180  ---it will give next 180 days date

select current_date + 180  ---it will give next 180 days date

select current_date - 180  ---it will give before 180 days date

Comments

1

I would suggest this if you need to add the number of days to the timestamp: SELECT (now() + (:count_days * INTERVAL '1 days'))::timestamp;

Comments

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.