postgresql_16:3.6.9.3 date_bin
PostgreSQL 16 > The SQL Language > Functions and Operators > Date/Time Functions and Operators > date_bin
The function date_bin “bins” the input timestamp into the specified interval (the *stride*) aligned with a specified origin.
date_bin(*stride*, *source*, *origin*) *source* is a value expression of type timestamp or timestamp with time zone. (Values of type date are cast automatically to timestamp.) *stride* is a value expression of type interval. The return value is likewise of type timestamp or timestamp with time zone, and it marks the beginning of the bin into which the *source* is placed.
Examples: SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01'); Result: 2020-02-11 15:30:00 SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30'); Result: 2020-02-11 15:32:30
In the case of full units (1 minute, 1 hour, etc.), it gives the same result as the analogous date_trunc call, but the difference is that date_bin can truncate to an arbitrary interval.
The stride interval must be greater than zero and cannot contain units of month or larger.