Home > Software engineering >  dateadd in postgresSQL
dateadd in postgresSQL

Time:05-19

I have this student_loan table and would like to know those that have finished early.

student-id, loan_amount, date_given, terms, last_paymnt
xxxx        xxxx         04/03/2010   36    08/09/2012
xxxx        xxxx         16/09/2011   45    13/12/2017

My logic is to grab to add the terms (currently integer) to the date_given as months to have a presumed finished date in a cte then grab another subtraction between that date and the given-date to have the result in months so I can the earlies and the lates.

I am trying the below but without success:

select date_given   interval 'terms month'
from Student_loan

How would I add that integer to the date and have a number of months or even subtract the two dates in my table and return a number of month?

CodePudding user response:

You can use make_interval()

select date_given   make_interval(months => terms)
from student_loan

Alternatively multiply a one month interval with the value of the column:

select date_given   terms * interval '1 month'
from student_loan

or even subtract the two dates in my table and return a number of month?

Just subtract them, the result of that is the number of days as both columns are (or at least seem to be) date values:

select last_paymnt - date_given as num_days
from student_loan
  • Related