// dates & times reference

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.

Categoria
Dates & times
Returns
integer
In this category
41 of 121

Sintaxe

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.

Suporte a mecanismos

MotorSupportNotas
SQL Server Different name or syntax DATEPART(minute, ts); no MINUTE() shorthand
PostgreSQL Different name or syntax EXTRACT(minute FROM ts)
MySQL Sim Supported under this name.
SQLite Different name or syntax strftime('%M', ts) returns text
Oracle Different name or syntax EXTRACT needs a TIMESTAMP

Exemplos

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;

Bom saber

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.