SQL MINUTE function
Returns the minutes component of a timestamp, the piece of bucketing that turns raw event times into half-hour or five-minute cohorts.
- Returns
integer- In this category
- 41 of 121
語法
EXTRACT(minute FROM ts)
DATEPART(minute, ts) -- SQL Server
MINUTE(ts) -- MySQL
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | DATEPART(minute, ts); no MINUTE() shorthand |
| PostgreSQL | Different name or syntax | EXTRACT(minute FROM ts) |
| MySQL | 是 | Supported under this name. |
| SQLite | Different name or syntax | strftime('%M', ts) returns text |
| Oracle | Different name or syntax | EXTRACT needs a TIMESTAMP |
範例
PostgreSQL
SELECT EXTRACT(hour FROM created_at) AS hr,
FLOOR(EXTRACT(minute FROM created_at) / 5) AS slot
FROM events;
MySQL
SELECT MINUTE(created_at) AS min FROM orders;
SQLite
SELECT CAST(strftime('%M', created_at) AS INTEGER) AS min FROM orders;
小知識
- Oracle's EXTRACT(minute) fails on plain DATE; cast to TIMESTAMP or use TO_CHAR(d, 'MI').
- For sub-hour buckets, floor the division: on PostgreSQL and MySQL EXTRACT(minute ...) / 5 returns 6.8 for minute 34, so wrap it in FLOOR(). PostgreSQL 14+ has date_bin for this.
- SQLite's strftime('%M') is minutes; %m is the month, and mixing them is the classic SQLite date bug.
Questions about MINUTE
Why does my SQLite query return months?
strftime('%m') is the month; minutes are %M. Capital and lowercase mean opposite things compared to what you might guess.
How do I bucket into 15-minute intervals?
MySQL: FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(ts) / 900) * 900). PostgreSQL 14+: date_bin('15 minutes', ts, TIMESTAMP '2000-01-01'). SQL Server 2022+: DATE_BUCKET(minute, 15, ts).
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, 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.