Hive常用日期函数,时间函数

Hive常用日期函数,时间函数

Hive常用日期函数,时间函数

  1. 月份增减 add_months()
-- 1. 月份增减 add_months(), 返回类型: STRING - date
SELECT add_months('2023-07-05', -2);  -- 2023-05-05
SELECT add_months('2023-07-05', 2); -- 2023-09-05
  1. 当前时间日期 current_date()
-- 2. 当前时间日期 current_date(), 返回类型:date
SELECT current_date(); -- 2023-07-05
  1. 当前时间戳 current_timestamp()
-- 3. 当前时间戳 current_timestamp(), 返回类型:timestamp - date time
SELECT current_timestamp(); -- 2023-07-05 11:17:56.314
  1. 日期间隔天数 datediff()
-- 4. 日期间隔天数 datediff(), 返回类型: int
SELECT datediff('2023-07-05', '2023-07-10'); -- -5
SELECT datediff('2023-07-05', '2023-06-05'); -- 30
  1. 日期增减 date_add()
-- 5. 日期增减 date_add(), 返回类型: string - date
SELECT date_add('2023-07-05', 8); -- 2023-07-13
SELECT date_add('2023-07-05', -8); -- 2023-06-27
  1. 时间戳返回指定日期格式 date_format()
-- 6. 时间戳返回指定日期格式 date_format(), 返回类型: string
SELECT date_format(current_date(), 'yyyy-MM');         -- 2023-07
SELECT date_format(current_timestamp(), 'yyyy-MM-dd'); -- 2023-07-05
SELECT date_format('2023-07-05 11:17:56.314', 'yyyy'); -- 2023
  1. 返回日期减少时间 date_sub()
-- 7. 返回日期减少时间 date_sub(), 返回类型: string
SELECT date_sub('2023-07-05', -2); -- 2023-07-07
SELECT date_sub('2023-07-05', 2);  -- 2023-07-03
  1. 返回时间的日期部分 day()
-- 8. 返回时间的日期部分 day(), 返回类型: int
SELECT day('2023-07-05 11:17:56.314'); -- 5
SELECT day('2023-07-05'); -- 5
  1. 返回时间的日期部分 dayfmonth()
-- 9. 返回时间的日期部分 dayfmonth(), 返回类型: int
SELECT dayofmonth('2023-07-05 11:17:56.314'); -- 5
SELECT dayofmonth('2023-07-05'); -- 5
  1. 将时间戳(10位)转为日期字符串格式 from_unixtime(bigint,‘格式’)
-- 10. 将时间戳(10位)转为日期字符串格式 from_unixtime(bigint,'格式'), 返回类型: string
SELECT from_unixtime(1688538070,'yyyy-MM-dd'); -- 2023-07-05
SELECT from_unixtime(1688538070,'yyyy-MM-dd HH:mm:ss'); -- 2023-07-05 14:21:10
  1. 将日期时间,转化为特定是去的时间 from_utc_timestamp()
-- 11. 将日期时间,转化为特定是去的时间 from_utc_timestamp(), 返回类型: 日期
SELECT from_utc_timestamp('2023-07-05 11:17:56.314','PST'); -- 2023-07-05 04:17:56.314
SELECT from_utc_timestamp('2023-07-05 11:17:56.314','GMT'); -- 2023-07-05 11:17:56.314
SELECT from_utc_timestamp('2023-07-05 11:17:56.314','UTC'); -- 2023-07-05 11:17:56.314
SELECT from_utc_timestamp('2023-07-05 11:17:56.314','DST'); -- 2023-07-05 11:17:56.314
SELECT from_utc_timestamp('2023-07-05 11:17:56.314','CST'); -- 2023-07-05 06:17:56.314
  1. 返回时间的小时部分 hour()
-- 12. 返回时间的小时部分 hour(), 返回类型: int
SELECT hour('2023-07-05 11:17:56.314'); -- 11
  1. 获取给定日期的当月的最后一天 last_day()
-- 13. 获取给定日期的当月的最后一天 last_day(), 返回类型: string
SELECT last_day('2023-07-05 11:17:56.314'); -- 2023-07-31
SELECT last_day('2023-07-05'); -- 2023-07-31
  1. minute() 返回时间的分钟值
-- 14. minute() 返回时间的分钟值, 返回类型: int
SELECT minute('2023-07-05 11:17:56'); -- 17
  1. month() 返回时间的月份值
-- 15. month() 返回时间的月份值, 返回类型: int
SELECT month('2023-07-05 11:17:56'); -- 7
  1. months_between() 返回两个时间之间的月份差
-- 16. 返回两个时间之间的月份差, 返回类型: double
SELECT months_between('2023-07-05','2023-06-05') ; -- 1.0
SELECT months_between('2023-07-05','2023-06-21') ; -- 0.48387097
  1. next_day() 返回指定日期的下一个周几日期
-- 17. next_day 返回指定日期的下一个周几日期, 返回类型: string
SELECT next_day('2023-07-05', 'Mon'); -- 返回当前日期的下一个周一日期:2023-07-10
SELECT next_day('2023-07-05', 'Tu');  -- 返回当前日期的下一个周二日期:2023-07-11
SELECT next_day('2023-07-05', 'Wed'); -- 返回当前日期的下一个周三日期:2023-07-12
SELECT next_day('2023-07-05', 'Th');  -- 2022-03-09为周三,指定参数为周四,所以返回当周的周四就是:2023-07-06
SELECT next_day('2023-07-05', 'Fri'); -- 返回当周周五日期:2023-07-07
SELECT next_day('2023-07-05', 'Sat'); -- 返回当周周六日期:2023-07-08
SELECT next_day('2023-07-05', 'Sun'); -- 返回当周周日日期:2023-07-09
  1. quarter() 返回指定日期的季度
-- 18. quarter() 返回指定日期的季度, 返回类型: int
SELECT quarter('2023-07-05') ; -- 3
  1. second() 返回给定时间的秒值
-- 19. second() 返回给定时间的秒值, 返回类型: int
SELECT second('2023-07-05 11:17:56.314') ; -- 56
  1. to_date() 返回给定时间的日期部分
-- 20. to_date() 返回给定时间的日期部分, 返回类型: date
SELECT to_date('2023-07-05 11:17:56.314') ; -- 2023-07-05
SELECT to_date('2023-07-05') ; -- 2023-07-05
  1. to_utc_timestamp() 转换成世界标准时间UTC的时间
-- 21. to_utc_timestamp 转换成世界标准时间UTC的时间, 返回类型: timestamp
SELECT to_utc_timestamp('2023-07-05 11:17:56.314','GMT') ; -- 2023-07-05 11:17:56.314
SELECT to_utc_timestamp('2023-07-05 11:17:56.314','PST') ; -- 2023-07-05 18:17:56.314
  1. trunc() 获取指定日期部
-- 22. trunc() 获取指定日期部分, 返回类型: string
SELECT trunc('2023-07-05 11:17:56','YY') ; -- 2023-01-01
SELECT trunc('2023-07-05 11:17:56','YYYY') ; -- 2023-01-01
SELECT trunc('2023-07-05 11:17:56','YEAR') ; -- 2023-01-01
SELECT trunc('2023-07-05 11:17:56','MM') ; -- 2023-07-01
SELECT trunc('2023-07-05 11:17:56','MON') ; -- 2023-07-01
SELECT trunc('2023-07-05 11:17:56','MONTH') ; -- 2023-07-01
  1. unix_timestamp() 返回日期的时间戳
-- 23. unix_timestamp() 返回日期的时间戳, 返回类型: bigint
SELECT unix_timestamp() ; -- 1688545018 当前时间
SELECT unix_timestamp('2023-07-05','yyyy-MM-dd') ; -- 1688486400
SELECT unix_timestamp('2023-07-05 11:17:56','yyyy-MM-dd hh:mm:ss') ; -- 1688527076 -- 小时小写会出现 'NULL'
SELECT unix_timestamp('2023-07-05 11:17:56','yyyy-MM-dd HH:mm:ss') ; -- 1688527076
  1. weekofyear() 返回指定日期在一年中的第几周
-- 24. weekofyear() 返回指定日期在一年中的第几周, 返回类型: int
SELECT weekofyear('2023-01-01') ; -- 52
SELECT weekofyear('2023-01-02') ; -- 1
SELECT weekofyear('2023-07-05') ; -- 27
SELECT weekofyear('2023-07-05 11:17:56') ; -- 27
  1. year() 返回时间的年份
-- 25. year() 返回时间的年份, 返回类型: int
SELECT year('2023-07-05') ; -- 2023
  1. extract() 提取指定值
-- 26. extract() 提取指定值 day, dayofweek, hour, minute, month, quarter, second, week, year 返回类型: int
SELECT extract(second from '2023-07-05 11:17:56') ; -- 56
SELECT extract(minute from '2023-07-05 11:17:56') ; -- 17
SELECT extract(hour from '2023-07-05 11:17:56') ; -- 11
SELECT extract(day from '2023-07-05 11:17:56') ; -- 5
SELECT extract(month from '2023-07-05 11:17:56') ; -- 7
SELECT extract(year from '2023-07-05 11:17:56') ; -- 2023
SELECT extract(week from '2023-07-05 11:17:56') ; -- 27
SELECT extract(quarter from '2023-07-05 11:17:56') ; -- 3
SELECT extract(dayofweek from '2023-07-05 11:17:56') ; -- 4

end

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值