跳到主要内容

TO_SECONDS

描述

秒数计算函数,它用于将日期转换为秒数值,即计算从零日期(0000-00-00)到指定日期时间的总秒数。

该函数与 mysql 中的 to_seconds 函数 行为一致。

语法

TO_SECONDS(`<date_or_time_expr>`)

参数

|----------------------------|-----------------------------| | <date_or_time_expr> | 输入的日期时间值,支持输入 date/datetime 类型,具体 datetime 和 date 格式请查看 datetime 的转换date 的转换 |

返回值

返回从零日期(0000-00-00)到指定日期时间的总秒数,类型为 BIGINT。

若输入为 NULL 则返回 NULL。

举例

-- 以`0000-00-00`为基准日期
select to_seconds('0000-01-01');
+--------------------------+
| to_seconds('0000-01-01') |
+--------------------------+
| 86400 |
+--------------------------+
-- 从 00-00算 1 天,共 24 * 3600 == 86400s

select to_seconds('2025-01-01'), to_seconds(20250101);
+--------------------------+----------------------+
| to_seconds('2025-01-01') | to_seconds(20250101) |
+--------------------------+----------------------+
| 63902908800 | 63902908800 |
+--------------------------+----------------------+

SELECT
to_seconds('2025-01-01 11:22:33') AS datetime_type,
to_seconds(20250101112233) AS int_type;
+---------------+-------------+
| datetime_type | int_type |
+---------------+-------------+
| 63902949753 | 63902949753 |
+---------------+-------------+

select to_seconds('9999-12-31 23:59:59.999999');
+------------------------------------------+
| to_seconds('9999-12-31 23:59:59.999999') |
+------------------------------------------+
| 315569519999 |
+------------------------------------------+

SELECT to_seconds(NULL);
+------------------+
| to_seconds(NULL) |
+------------------+
| NULL |
+------------------+