0

I'm not sure how to check for date ranges using a postgres function. What I want to do is check if a date falls within a certain range (with leeway of a week before the starting date)

So basically, I want to check if a date is between 7 days before to current date, and if so I'll return the id of that row.

create or replace function eight(_day date) returns text as $$
declare
  r record;
  check alias for $1;
  startDate date;
begin
    for r in
    select * from terms
    order by starting;
  loop
    startDate := r.starting;
    if check between (..need help to create 7 days before startDate) and startDate return r.id;


end;
$$ language plpgsql;

I also have to check if the previous record's ending date collides with the startDate - 7days. How would I check the previous record?

1

2 Answers 2

1

Sounds like you want to use an interval:

startDate - interval '...'

I won't say any more than this since you're doing homework.

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

4 Comments

i was using if check between startDate - interval '7 days' but I'm not sure if that works
@SNpn: Why don't you think between startDate - interval '7 days' and startDate works?
i wasn't sure if the '7 days' part would work, also could you read my edit in OP - bit stuck on how to tackle that.
Added my answer. Just do between (startdate - 7) and startdate or the like. Dates work with integer math, where integers represent days.
0

Dates work with integer math.

startdate - 8 is equivalent to (startdate::timestamp - '8 days'::interval)::date

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.