Dynamic (Column Based) Interval

Postgresql

Postgresql Problem Overview


How do I add a dynamic (column based) number of days to NOW?

SELECT NOW() + INTERVAL a.number_of_days "DAYS" AS "The Future Date" 
FROM a;

Where a.number_of_days is an integer?

Postgresql Solutions


Solution 1 - Postgresql

I usually multiply the number by interval '1 day' or similar, e.g.:

select now() + interval '1 day' * a.number_of_days from a;

Solution 2 - Postgresql

I know this is a year old, but if you need to use a column to specify the actual interval (e.g. 'days', 'months', then it is worth knowing that you can also CAST your string to an Interval, giving:

SELECT now()+ CAST(the_duration||' '||the_interval AS Interval)

So the the original question would become:

SELECT now() + CAST(a.number_of_days||" DAYS" AS Interval) as "The Future Date" FROM a;

Solution 3 - Postgresql

I prefer this way. I think its pretty easy and clean. In postgre you need interval to use + operator with timestamp

select (3||' seconds')::interval;

select now()+ (10||' seconds')::interval,now();

where you can use seconds,minutes...days,months... and you can replace the numbers to your column.

select now()+ (column_name||' seconds')::interval,now()
from your_table;

Solution 4 - Postgresql

To creating intervals those based on column values, I recommend to add two columns in your table. For example, column "period_value"::INT4 and column "period_name"::VARCHAR. Column "period_name" can store the following values:

  • microsecond
  • milliseconds
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
  • decade
  • century
  • millennium

+--------------+-------------+
| period_value | period_name |
+--------------+-------------+
| 2            | minute      |
+--------------+-------------+

Now you can write:

SELECT NOW() - (period_value::TEXT || ' ' || period_name::TEXT)::INTERVAL FROM table;

Solution 5 - Postgresql

Use make_interval()

SELECT NOW() + make_interval(days => a.number_of_days) AS "The Future Date" 
FROM a;

But in general it might be a better idea to use a column defined as interval, then you can use any unit you want when you store a value in there.

Solution 6 - Postgresql

If we have field with interval string value such as '41 years 11 mons 4 days' and want to convert it to date of birth use this query :

UPDATE "february14" set dob = date '2014/02/01'  - (patient_age::INTERVAL) 

dob is date field to convert '41 years 11 mons 4 days' to '1972/10/14' for example
patient_age is varchar field that have string like '41 years 11 mons 4 days'

And this is query to convert age back to date of birth

SELECT now() - INTERVAL '41 years 10 mons 10 days';

Solution 7 - Postgresql

Updating based on a column ID was a useful way to create some randomised test data for me.

update study_histories set last_seen_at = now() - interval '3 minutes' * id;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionexplodesView Question on Stackoverflow
Solution 1 - PostgresqlaraqnidView Answer on Stackoverflow
Solution 2 - PostgresqlPaul SView Answer on Stackoverflow
Solution 3 - PostgresqlSüniÚrView Answer on Stackoverflow
Solution 4 - PostgresqlIgor Manturov Jr.View Answer on Stackoverflow
Solution 5 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 6 - PostgresqlMohammad YusufView Answer on Stackoverflow
Solution 7 - Postgresqlport5432View Answer on Stackoverflow