大网数据广告活动
数据库部署与 SQL 参考

Apache Doris 中文参考:UNIX_TIMESTAMP

· 52CE 我爱测

1. 输入返回带 9 位小数的 时间戳。 2. scale 非 0 的 输入,或带 的调用,返回最多 6 位小数的 时间戳。 3. 不带 的 或 scale 为 0 的 输入返回 时间戳。

描述

将 DATE、DATETIME 或 TIMESTAMP_NS 值转换为 Unix 时间戳。

如果没有参数,则是将当前的时间转化为时间戳。

提供日期时间参数时,其类型必须为 DATE、DATETIME 或 TIMESTAMP_NS。 Format 的格式请参阅 date_format 函数的格式说明。

该函数受时区影响,时区部分请查看 时区管理。

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

语法


UNIX_TIMESTAMP()
UNIX_TIMESTAMP(<date_or_time_expr>)
UNIX_TIMESTAMP(<date_or_time_expr>[, fmt])

参数

参数 描述
<date_or_time_expr> 输入的日期时间值,支持 DATE、DATETIME 和 TIMESTAMP_NS。
<fmt> date 参数指代需要转换为时间戳的特定部分,其类型为 string。若提供该参数,则仅将与格式匹配的部分转换为时间戳。

返回值

返回类型取决于输入:

  1. TIMESTAMP_NS 输入返回带 9 位小数的 DECIMAL 时间戳。
  2. scale 非 0 的 DATETIME 输入,或带 <fmt> 的调用,返回最多 6 位小数的 DECIMAL 时间戳。
  3. 不带 <fmt> 的 DATE 或 scale 为 0 的 DATETIME 输入返回 INT 时间戳。

将输入时间转换为当前输入时间所对应的时间戳,起始时间为 1970-01-01 00:00:00.

  • 任意参数为 null 则返回 null
  • 无效格式,返回错误

举例

---输入时间为起始日期时间,返回 0
mysql> select unix_timestamp('1970-01-01 00:00:00');
+------------------------------+
| unix_timestamp('1970-01-01') |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (0.03 sec)

---显示当前时间的时间戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
|       1753933330 |
+------------------+

---输入一个 datetime 显示该时间的
mysql> select unix_timestamp('2007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30:19') |
+---------------------------------------+
|                            1196389819 |
+---------------------------------------+

---匹配 format 显示给出的 datetime 对应时间戳
mysql> select unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s');
+------------------------------------------------------------+
| unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s') |
+------------------------------------------------------------+
|                                          1196389819.000000 |
+------------------------------------------------------------+

---只匹配年日月显示时间戳
mysql> select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d');
+-----------------------------------------------------+
| unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d') |
+-----------------------------------------------------+
|                                   1196352000.000000 |
+-----------------------------------------------------+

---带有其他字符匹配
mysql> select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s');
+-----------------------------------------------------------------+
| unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s') |
+-----------------------------------------------------------------+
|                                               1196389819.000000 |
+-----------------------------------------------------------------+

---输入时间并且 scale 不为 0
mysql> SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19.123');
+-------------------------------------------+
| UNIX_TIMESTAMP('2015-11-13 10:20:19.123') |
+-------------------------------------------+
|                            1447381219.123 |
+-------------------------------------------+

---在1970-01-01 之前的日期时间,返回 0
select unix_timestamp('1007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('1007-11-30 10:30:19') |
+---------------------------------------+
|                                     0 |
+---------------------------------------+

---任意参数为 null 则返回 Null
mysql> select unix_timestamp(NULL);
+----------------------+
| unix_timestamp(NULL) |
+----------------------+
|                 NULL |
+----------------------+

mysql> select unix_timestamp('2038-01-19 11:14:08',null);
+--------------------------------------------+
| unix_timestamp('2038-01-19 11:14:08',null) |
+--------------------------------------------+
|                                       NULL |
+--------------------------------------------+

--无效格式,返回错误
mysql> select unix_timestamp('2007-11-30 10:30-19', 's');
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]Operation unix_timestamp of 2007-11-30 10:30-19, s is invalid

本文来自 Apache Doris 文档贡献者,按 Apache 2.0 整理版式与相对链接,保留正文与代码。图示请参阅原文。作者与贡献记录。

资料来源与适用范围

资料来源
Apache Doris 文档贡献者
适用范围
master 文档快照(2026-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
Apache 2.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。