본문 바로가기
DB/Postgresql

postgres column값을 interval에 다이나믹하게 사용하는 방법

by fabxoe 2020. 7. 22.

https://stackoverflow.com/questions/5266758/dynamic-column-based-interval

 

Dynamic (Column Based) Interval

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?

stackoverflow.com

 

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)

 

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

댓글