SQL AGE function
Returns the interval between two timestamps, broken into years, months and days, built for ages and tenure, not stopwatch timing.
- 카테고리
- Dates & times
- Returns
interval- In this category
- 68 of 121
구문
AGE(timestamp, timestamp)
AGE(timestamp)
Each block is a separate form. The support table and the examples show which engine takes which.
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | 예 | Supported under this name. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Different name or syntax | MONTHS_BETWEEN plus arithmetic, or timestamp subtraction |
예시
PostgreSQL
SELECT AGE(TIMESTAMP '2026-08-01', TIMESTAMP '2020-01-15'); -- 6 years 6 mons 17 days
PostgreSQL
SELECT AGE(birth_date) FROM people;
알아 두면 좋은 정보
- AGE returns calendar fields, not a length of time: AGE('2026-03-01', '2026-02-01') is 1 mon, not 28 days. Read whole years with EXTRACT(YEAR FROM AGE(...)).
- The single-argument form subtracts from CURRENT_DATE at midnight, so AGE(ts) counts whole days from today, not from the current time.
- Negative results are signed per field: AGE('1999-01-01', '2000-01-15') gives -1 years -14 days, not one negative total.
Questions about AGE
How do I get someone's age in years outside PostgreSQL?
MySQL: TIMESTAMPDIFF(YEAR, birth, CURDATE()) counts completed years. Oracle: TRUNC(MONTHS_BETWEEN(SYSDATE, birth) / 12). SQL Server: DATEDIFF(YEAR, ...) counts year boundaries, so subtract 1 when this year's birthday has not happened yet.
Does AGE keep hours and minutes?
Yes, when the inputs have them: AGE(TIMESTAMP '2026-08-01 18:00', TIMESTAMP '2026-08-01 06:30') is 11:30:00. Use plain subtraction (ts1 - ts2) when you want days and time without the month split.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Compute age in years, months and days in SQL Snippet
- Find the next anniversary of a date in SQL Snippet
Cheat sheets for the engines that have it: PostgreSQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.