0

I have a set of data that looks similar to this:

enter image description here

What I'm trying to do is create a third field, let's call it length, which takes the created_date field and gives you a date field based on created_date + months

So in practicality, that third column would look something like this

enter image description here

But when I try the way I looked up, it gives me an error:

SELECT
DATEADD('month', months, DATE(created_date)) 
FROM
table

ERROR: function date_add(unknown, double precision, date) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.

Am I just using the wrong type of syntax and there's a different function I should be using?

1 Answers1

1

Note: based on the error message I assume you are using Postgres.


You can add an interval directly to a date value. As you have a fixed unit, using make_interval() is the easiest way:

SELECT date_created + make_interval(months => "months"::integer)
FROM the_table

The cast to an integer is necessary, because you can't specify fractional months using that function. If you really need that, then multiply the value with an interval of one month:

SELECT date_created + months * interval '1 month'
FROM the_table

In general I would recommend to change the months column to an interval then you don't need to worry about things like that.