Date and Time Operators
When the user uses date/time operators, explicit type prefixes are modified for corresponding operands to ensure that the operands parsed by the database are consistent with what the user expects, and no unexpected results occur.
For example, abnormal mistakes will occur in the following example without an explicit data type.
1 2 |
SELECT date '2001-10-01' - '7' AS RESULT; ERROR: invalid input syntax for type timestamp: "7" |
Operator |
Example |
||||||
---|---|---|---|---|---|---|---|
+ |
Add a date with an integer to obtain the date after 7 days.
|
||||||
Add a date with an interval to obtain the time after 1 hour.
|
|||||||
Add a date with a time to obtain a specific time.
|
|||||||
Add a date with an interval to obtain the time after one month. If the sum or subtraction results fall beyond the date range of a month, the result will be rounded to the last day of the month. For example, if the date of the month after 2021-01-31 is 2021-02-31, but February is a leap month and has only 28 days, the date function will accordingly return the last day of February, that is, 2021-02-28.
|
|||||||
Add two intervals to obtain the sum.
|
|||||||
Add a timestamp with an interval to obtain the time after 23 hours.
|
|||||||
Add a time with an interval to obtain the time after three hours.
|
|||||||
- |
Subtract a date from another to obtain the difference.
|
||||||
Subtract an integer from a date, the return is a timestamp type.
|
|||||||
Subtract an interval from a date to obtain the time difference.
|
|||||||
Subtract a time from another time to obtain the time difference.
|
|||||||
Subtract an interval from a time to obtain the time difference.
|
|||||||
Subtract an interval from a timestamp to obtain the date difference.
|
|||||||
Subtract an interval from another interval to obtain the time difference.
|
|||||||
Subtract a timestamp from another timestamp to obtain the time difference.
|
|||||||
Obtain the time at the previous day.
|
|||||||
* |
Multiply an interval by a quantity:
|
||||||
/ |
Divide an interval by a quantity to obtain a time segment.
|
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.