メインコンテンツまでスキップ

TO_SECONDS

説明

秒数計算関数。日付を秒数値に変換し、基準日(0000-00-00)から指定された日付までの総秒数を計算します。

この関数はMySQLのto_seconds functionと一貫した動作をします。

構文

TO_SECONDS(`<date_or_time_expr>`)

パラメータ

パラメータ説明
<date_or_time_expr>入力する日付/時刻の値。date/datetimeタイプをサポートします。具体的なdatetimeおよびdateの形式については、datetime conversionおよびdate conversionを参照してください。

戻り値

ゼロ日付(0000-00-00)から指定された日付と時刻までの総秒数をBIGINT型で返します。

入力がNULLの場合、NULLを返します。

-- Based on the date `0000-00-00`
select to_seconds('0000-01-01');
+--------------------------+
| to_seconds('0000-01-01') |
+--------------------------+
| 86400 |
+--------------------------+
-- Starting from 00-00, count 1 day, a total of 24 * 3600 == 86400 seconds.

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 |
+------------------+