メインコンテンツまでスキップ
に最終更新

TO_SECONDS

説明​

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

この関数は、MySQL の to_seconds 関数 と同じ動作をします。

構文​

TO_SECONDS(`<date_or_time_expr>`)

パラメータ​

パラメータ説明
<date_or_time_expr>入力する日付または日時の値です。DATE 型と DATETIME 型をサポートします。具体的な日時と日付の形式については、日時の変換および日付の変換を参照してください。

戻り値​

ゼロ日付(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 |
+------------------+