Best April deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dealing with dates
Working with time
Date and time data types in PostgreSQL
Extracting dates and times
Intervals
Timezone conversion
Format date and time
Current date and time data
Summary

Instruction

The addition failed because our database thinks that '2015-01-01' is a common text value (string) and it's not able to add an interval to a text field. What can we do then? We can explicitly convert '2015-01-01' to a date/timestamp:

SELECT '2015-01-01'::timestamp + INTERVAL '5 days';

PostgreSQL delivers the double colon operator (::) to convert one data type to another. On the left, we have the value to be converted to another data typem specified by the data type on the right. In this example, the string '2015-01-01' is converted to a timestamp data type. Now, it's possible to add an interval (5 days) to the date and time. In the same way, we can convert strings containing dates to date data type. Look at the another example:

SELECT '2015-01-01'::date + INTERVAL '5 day';

Let's see if it works.

Exercise

Run the new template to see if this query works.