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

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

相关推荐
2601_962780911 小时前
2027校招准备:统计学专业如何用数据能力应聘企业经营管理管培生
大数据
阿明副业观察2 小时前
AI视频生成技术全解:生成方法、操作流程与选型指南
大数据·人工智能·ai作画·aigc·ai写作
金伟API10243 小时前
不会写复杂SQL怎么办?
数据库·sql
Leo.yuan3 小时前
FineBI AI原生版:Agent主动规划分析路径,让数据成为企业经营决策的顾问
大数据
geovindu3 小时前
sql:Search-Optimized SQL Queries using mysql 9.0
android·数据库·sql·mysql·数据库开发·数据库架构
VibeCoding工程之道4 小时前
第八章 最终格局:不是 AI 淘汰人,是「依赖 AI 的人」淘汰自己
大数据·人工智能·ai焦虑·ai思考·ai反噬
大大大大晴天4 小时前
每天认识一个组件:Apache Kafka
大数据
2501_933670794 小时前
2027秋招岗位能力分组:数据科学与大数据技术专业如何组织技能与项目
大数据
野生数据人5 小时前
日志平台每日巡检我压成了一条命令链:从 SHOW BACKENDS 到扩缩容的完整步骤
大数据·数据库
liliangcsdn6 小时前
如何用DrissionPage进行网页自动化
大数据·自动化