Dateadd Equivalent in Postgresql [Duplicate]

Dateadd Equivalent in Postgresql [Duplicate]

Is there an equivalent to this T-SQL command in PostgreSQL?

select dateadd(hh,duration_in_hours,start_date) as end_date

I have found only interval keyword with subsequent string, but this terrible construction returns syntax error:

select start_date + interval cast(duration_in_hours as varchar) || ' hours'

It allows only string constant after "interval " keyword. I am sure there must be some similar function in pgsql, but I cannot find it.

3

1 Answer

You can do it like this:

select start_date + (duration_in_hours * interval '1 hour') from your_table

See this sample SQL Fiddle

2
Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.