Hive-时间日期&trunc-日期与数字截取函数

一、时间日期函数

1、获取当前时间
1current_timestamp()	-- 获取时间
	2022-10-09 16:00:24.189
2、unix_timestamp() 		-- 获取时间戳
	1665302498
3select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')		-- 获取指定格式的日期
	2022-10-09 16:03:00
2、日期转时间戳
1SELECT FROM_UNIXTIME(1664208000,'yyyy-MM-dd') -- 时间戳转日期

2select unix_timestamp()   -- 获取当前时间戳

3select unix_timestamp('2021-11-11 11:11:11'); --1636600271 -- 日期转时间戳
unix_timestamp()  -- 输入日期参数  输入的时间格式必须符合 yyyy-MM-dd HH:mm:ss
3、时间戳转日期
1、from_unixtime()  -- 将时间戳转化为指定的时间格式

2select from_unixtime(1628956800,'yyyy-MM-dd'); -- 2021-08-15

3select from_unixtime(1628956800,'yyyy-MM-dd HH-mm'); -- 2021-08-15 00-00

二、trunc - 日期与数字截取函数

1、日期截取
TRUNC函数为指定元素而截去的日期值。
其具体的语法格式如下:
TRUNC(date[,fmt])
其中:date 一个日期值
fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
如果当日日期是:2011-3-18

select trunc(sysdate) from dual --2011-3-18 今天的日期为2011-3-18
select trunc(sysdate, 'mm') from dual --2011-3-1 返回当月第一天.
select trunc(sysdate,'yy') from dual --2011-1-1 返回当年第一天
select trunc(sysdate,'dd') from dual --2011-3-18 返回当前年月日
select trunc(sysdate,'yyyy') from dual --2011-1-1 返回当年第一天
select trunc(sysdate,'d') from dual --2011-3-13 (星期天)返回当前星期的第一天
select trunc(sysdate, 'hh') from dual --2011-3-18 14:00:00 当前时间为14:41
select trunc(sysdate, 'mi') from dual --2011-3-18 14:41:00 TRUNC()函数没有秒的精确
2、数字截取
TRUNC(number,num_digits)
Number 需要截尾取整的数字。
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。
TRUNC()函数截取时不进行四舍五入

select trunc(123.458) from dual --123
select trunc(123.458,0) from dual --123
select trunc(123.458,1) from dual --123.4
select trunc(123.458,-1) from dual --120
select trunc(123.458,-4) from dual --0
select trunc(123.458,4) from dual --123.458
select trunc(123) from dual --123
select trunc(123,1) from dual --123
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值