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

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

相关推荐
果丁智能22 分钟前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
王小王-12339 分钟前
基于 Hive 的网易云音乐数据分析及可视化系统
hive·hadoop·数据分析·音乐数据分析·网易云音乐分析·hive音乐分析·hadoop网易云
ApacheSeaTunnel1 小时前
实战演示 | 基于 Apache SeaTunnel 与 Apache DolphinScheduler 实现 MySQL 到 Doris 离线定时增量同步
大数据·mysql·开源·doris·数据集成·seatunnel·数据同步
weixin_397574091 小时前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
极光代码工作室2 小时前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
BD_Marathon2 小时前
SQL学习指南——视图
数据库·sql
秋名山码民2 小时前
Graph RAG 深度解析:从向量检索到知识推理的技术演进
大数据·人工智能·rag
2601_962072552 小时前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos
Database_Cool_3 小时前
大规模数据分析降本指南:AnalyticDB Serverless 弹性架构实战
数据仓库·阿里云·架构·数据分析·serverless
m0_380167143 小时前
面向开发者的Top10加密货币数据API(2026年最新)
大数据·人工智能·区块链