Hive常用函数_16个时间日期处理

在Hive中,常用的时间处理函数包括但不限于以下几种:

1. current_date(): 返回当前日期,不包含时间部分
c 复制代码
SELECT current_date();
-- Output: 2024-09-15
2. current_timestamp(): 返回当前时间戳,包含日期和时间部分
c 复制代码
SELECT current_timestamp();
-- Output: 2024-09-15 12:34:56
3. year(date): 提取日期中的年份部分
c 复制代码
SELECT year('2024-09-15');
-- Output: 2024
4. month(date): 提取日期中的月份部分
c 复制代码
SELECT month('2024-09-15');
-- Output: 9
5. day(date): 提取日期中的天数部分
c 复制代码
SELECT day('2024-09-15');
-- Output: 15
6. date_format(date, pattern): 将日期按照指定的模式格式化输出
c 复制代码
SELECT date_format('2024-09-15', 'yyyy-MM-dd');
-- Output: '2024-09-15'
7. from_unixtime(unix_time, format): 将Unix时间戳转换为指定格式的日期时间
c 复制代码
SELECT from_unixtime(1731692800, 'yyyy-MM-dd HH:mm:ss');
-- Output: '2024-09-15 00:00:00'
8. unix_timestamp(): 返回当前时间的Unix时间戳
c 复制代码
SELECT unix_timestamp();
-- Output: 1711537815
9. datediff(endDate, startDate): 计算两个日期之间的天数差
c 复制代码
SELECT datediff('2024-09-20', '2024-09-15');
-- Output: 5
10. date_add(date, days): 将指定天数添加到日期
c 复制代码
SELECT date_add('2024-09-15', 3);
-- Output: '2024-09-18'
11. date_sub(date, days): 从日期中减去指定天数
c 复制代码
SELECT date_sub('2024-09-15', 2);
-- Output: '2024-09-13'
12. last_day(date): 返回给定日期所在月份的最后一天
c 复制代码
SELECT last_day('2024-09-15');
-- Output: '2024-09-30'
13. to_date(timestamp): 将时间戳转换为日期
c 复制代码
SELECT to_date('2024-09-15 12:34:56');
-- Output: '2024-09-15'
14. hour(timestamp): 提取时间戳中的小时部分
c 复制代码
SELECT hour('2024-09-15 12:34:56');
-- Output: 12
15. minute(timestamp): 提取时间戳中的分钟部分
c 复制代码
SELECT minute('2024-09-15 12:34:56');
-- Output: 34
16. second(timestamp): 提取时间戳中的秒部分
c 复制代码
SELECT second('2024-09-15 12:34:56');
-- Output: 56

这些函数可用于处理日期和时间数据,进行日期的提取、格式化、计算以及转换等操作。根据实际需求,选择合适的时间处理函数可以方便地对日期时间数据进行处理和分析。

相关推荐
远光九天1 小时前
远光软件亮相2026 CIO百人会高峰论坛 分享“模数共振”新范式构建之路
大数据·人工智能
LitchiCheng2 小时前
DGX Spark部署Isaac Sim(一键安装脚本)
大数据·elasticsearch·spark
RockHopper20253 小时前
企业智能体为何应面向“活动”而非“数据”——一个架构范式的转变
大数据·人工智能·智能体·活动驱动
JustNow_Man3 小时前
【openspec】存量项目中如何使用进行开发
大数据·人工智能
Geeys4 小时前
拼多多新店快速起流量完整实操
大数据·网络·人工智能
BD_Marathon5 小时前
什么是Spark
大数据·分布式·spark
xlrqx6 小时前
2026年沧州家电清洗培训成本核算方法及成本控制要点
大数据·python
STLearner6 小时前
ICML 2026 | 时间序列(Time Series)论文总结(3)【因果,可解释性,不规则时序,表示学习,benchmar
大数据·论文阅读·人工智能·深度学习·神经网络·机器学习
Csvn6 小时前
📊 SQL 入门 Day 1:SELECT & WHERE — 数据查询的基本功
后端·sql
TDengine (老段)6 小时前
TDengine SMA 索引 — 块级/文件级统计索引
android·大数据·服务器·数据库·人工智能·时序数据库·tdengine