Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create interval using a column value postgresql

Tags:

sql

postgresql

I have a query that's something like this

select days, date(date_event)  + interval '10' day from tbl_user_marketing_program as programtable 

Now in place of the '10', I want to add the value present in the "days" column. How can I do that?

I tried select user_id, date(date_event) + interval user_id day from tbl_user_marketing_program as programtable

then I got the below error

ERROR: syntax error at or near "day" LINE 1: ...ect user_id, date(date_event) + interval user_id day from t...

like image 983
i_raqz Avatar asked Dec 05 '22 19:12

i_raqz


1 Answers

Unfortunately the "number" for an interval can't be an arbitrary expression, it has to be a string constant (which is a strange choice). You need to use a little workaround:

select days, date(date_event) + (days * interval '1' day) 
from tbl_user_marketing_program as programtable 

But date + integer is also directly supported and the unit is days in that case. So you can als write:

select days, date(date_event) + days 
from tbl_user_marketing_program as programtable 
like image 78
a_horse_with_no_name Avatar answered Dec 18 '22 00:12

a_horse_with_no_name