SECOND_CEIL
Description
SECOND_CEIL function rounds the input datetime value up to the nearest specified second period. If origin is specified, it uses that as the basis; otherwise, it defaults to 0001-01-01 00:00:00. The function supports processing DATETIME type.
Date calculation formula:
represents the number of periods needed to reach the target time from the base time.
Syntax
SECOND_CEIL(<datetime>[, <period>][, <origin_datetime>])
Parameters
| Parameter | Description |
|---|---|
<datetime> | Required. The input datetime value. Supports input of date/datetime/timestamptz types. For specific formats please see timestamptz的转换, datetime conversion and date conversion. |
<period> | Optional. Indicates how many seconds make up each period. Supports positive integer type (INT). Default is 1 second. |
<origin_datetime> | Optional. The alignment starting point. Supports input of datetime type and strings that conform to datetime formats. If not specified, defaults to 0001-01-01T00:00:00. |
Return Value
Returns a value of type TIMESTAMPTZ, DATETIME or DATE. Returns the time value after rounding up to the nearest specified second period based on the input datetime. The precision of the return value matches the precision of the input datetime parameter.
- If the input is TIMESTAMPTZ type, it will first be converted to local_time (for example:
2025-12-31 23:59:59+05:00represents local_time2026-01-01 02:59:59when the session variable is+08:00), and then perform SECOND_CEIL calculation. - If the input time values (
<date_or_time_expr>and<period>) contain both TIMESTAMPTZ and DATETIME types, the output is DATETIME type. - If
<period>is a non-positive integer (≤0), returns an error. - If any parameter is NULL, returns NULL.
- When period is not specified, defaults to a 1-second period.
- When
<origin>is not specified, defaults to 0001-01-01 00:00:00 as the basis. - If the input is DATE type (only contains year, month, day), its time portion defaults to 00:00:00.
- If the calculation result exceeds the valid range of DATETIME type (0000-01-01 00:00:00 to 9999-12-31 23:59:59.999999), returns an error.
- For datetime with scale, all decimal places are truncated to 0.
- If the
<origin>date and time is after the<period>, it will still be calculated according to the above formula, but the period k will be negative.
Examples
-- Default period of 1 second, default starting time 0001-01-01 00:00:00
SELECT SECOND_CEIL('2025-01-23 12:34:56') AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:34:56 |
+---------------------+
-- 5-second period, upward rounding result with default starting point
SELECT SECOND_CEIL('2025-01-23 12:34:56', 5) AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:35:00 |
+---------------------+
-- Only with origin date and specified date
select second_ceil("2023-07-13 22:28:18", "2023-07-13 22:13:12.123");
+---------------------------------------------------------------+
| second_ceil("2023-07-13 22:28:18", "2023-07-13 22:13:12.123") |
+---------------------------------------------------------------+
| 2023-07-13 22:28:18.123 |
+---------------------------------------------------------------+
-- Specify starting time (origin)
SELECT SECOND_CEIL('2025-01-23 12:34:56', 10, '2025-01-23 12:00:00') AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:35:00 |
+---------------------+
-- If the <origin> date and time is after the <period>, it will still be calculated according to the above formula, but the period k will be negative.
SELECT SECOND_CEIL('2025-01-23 12:34:56', 10, '2029-01-23 12:00:00') AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 12:35:00 |
+---------------------+
-- Datetime with microseconds, decimal places truncated to 0 after rounding
SELECT SECOND_CEIL('2025-01-23 12:34:56.789', 5) AS result;
+----------------------------+
| result |
+----------------------------+
| 2025-01-23 12:35:00.000000 |
+----------------------------+
-- Input is DATE type (default time 00:00:00)
SELECT SECOND_CEIL('2025-01-23', 30) AS result;
+---------------------+
| result |
+---------------------+
| 2025-01-23 00:00:00 |
+---------------------+
-- TimeStampTz sample, SET time_zone = '+08:00'
-- Convert to local_time (2026-01-01 02:59:59) and then perform SECOND_CEIL
SELECT SECOND_CEIL('2025-12-31 23:59:59+05:00');
+------------------------------------------+
| SECOND_CEIL('2025-12-31 23:59:59+05:00') |
+------------------------------------------+
| 2026-01-01 02:59:59+08:00 |
+------------------------------------------+
-- If parameters contain both TimeStampTz and Datetime types, output DateTime type
SELECT SECOND_CEIL('2025-12-31 23:59:59+05:00', '2025-12-15 00:00:00.123');
+---------------------------------------------------------------------+
| SECOND_CEIL('2025-12-31 23:59:59+05:00', '2025-12-15 00:00:00.123') |
+---------------------------------------------------------------------+
| 2026-01-01 02:59:59.123 |
+---------------------------------------------------------------------+
-- Calculation result exceeds maximum datetime range, returns error
SELECT SECOND_CEIL('9999-12-31 23:59:59', 2) AS result;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation second_ceil of 9999-12-31 23:59:59, 2 out of range
-- Period is non-positive, returns error
mysql> SELECT SECOND_CEIL('2025-01-23 12:34:56', -3) AS result;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation second_ceil of 2025-01-23 12:34:56, -3 out of range
-- Any parameter is NULL, returns NULL
SELECT SECOND_CEIL(NULL, 5), SECOND_CEIL('2025-01-23 12:34:56', NULL) AS result;
+------------------------+--------+
| second_ceil(NULL, 5) | result |
+------------------------+--------+
| NULL | NULL |
+------------------------+--------+