Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATEADD equivalent in PostgreSQL [duplicate]

Tags:

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.

like image 911
Hink Avatar asked Nov 22 '14 17:11

Hink


People also ask

Does Postgres have Dateadd?

Q. Where are the DATEADD and DATEDIFF functions in PostgreSQL? A. There are none.

What is Date_add in MySQL?

The DATE_ADD() function adds a time/date interval to a date and then returns the date.

How do I write a cast in PostgreSQL?

PostgreSQL supports a CAST operator that is used to convert a value of one type to another. Syntax: CAST ( expression AS target_type ); Let's analyze the above syntax: First, specify an expression that can be a constant, a table column, an expression that evaluates to a value.


1 Answers

You can do it like this:

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

See this sample SQL Fiddle

like image 65
jpw Avatar answered Sep 28 '22 00:09

jpw