INTERVAL (time span)

Last updated: 2026-11-04

INTERVAL (time span)

INTERVAL represents a span of time, stored internally as separate month, day and time components. That split lets it express calendar amounts whose length varies, such as "1 year 2 months", alongside exact durations such as "36 hours". Since PostgreSQL 14 the default output uses ISO 8601 style. Adding one to a TIMESTAMP yields a new timestamp, which makes it the most useful type for time arithmetic.

Kindtype
Example LanguageSQL

📝 Syntax

INTERVAL  ——  time span, stored separately as months / days / time-of-day
INTERVAL '1 day 2 hours'  ——  literal form
INTERVAL '1-2'  ——  ISO 8601 year-month form: 1 year 2 months

Returns:Stores a time span. Adding one to a TIMESTAMP or a DATE advances it by calendar rules (one month lands on the end of a shorter month), while adding one to a TIME affects only the time-of-day part.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

 id |      span      |     longer     |   normalized   | col_type
----+----------------+----------------+----------------+----------
  1 | 1 day 02:00:00 | 1 day 02:30:00 | 1 day 02:00:00 | interval
  2 | 01:30:00       | 02:00:00       | 01:30:00       | interval

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy is adding a month not the same as adding 30 days?
ABecause months are calendar units of varying length. PostgreSQL advances the month count and clamps the day number to the target month's range, which matches how billing and subscription periods are understood and stays consistent with leap-year rules.
QHow do you compare two intervals?
AIntervals compare directly with each other and with TIMESTAMPs for ordering. For exact duration arithmetic, converting with extract(epoch FROM span) to seconds is the most reliable route.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏