impala常用日期格式转换

impala常用日期格式转换 (单独测试可去除末尾的 from tableName)

一、获取当前日期

//获取当前日期
select from_unixtime(unix_timestamp(),'yyyy-MM-dd') from tableName;

二、日期格式化

//格式化日期
select from_unixtime(unix_timestamp('2021-11-12 11:12:11'),'yyyy-MM-dd') from tableName;

select from_unixtime(unix_timestamp(t1.column),'yyyy-MM-dd') from tableName t1;

三、获取半年前的时间

//获取半年前的时间
select add_months(from_unixtime(unis_timestamp(),'yyyy-MM-dd'),-6) from tableName;

四、当前时间减去30天

//当前时间减去30天
select from_unixtime(unix_timestamp()-60*60*24*30) from tableName

五、常用日期函数汇总

//增加月份
add_months(timestamp date, int months)
add_months(timestamp date, bigint months)
Return type: timestamp
usage:add_months(now(),1)

//增加日期
adddate(timestamp startdate, int days),
adddate(timestamp startdate, bigint days)
Return type: timestamp
usage:adddate(now(),1)

//当前时间戳
current_timestamp()和now()等价

//日期相减
datediff(string enddate, string startdate)
Return type:int
usage:datediff('2021-11-21', '2021-11-01')

//得到天,得到月份
day(string date)
Return type: int
usage: day('2021-11-21')

//得到星期英文
dayname(string date)
Return type: string
usage:dayname('2021-11-21') Sunday

//得到这一天是这周的第几天
dayofweek(string date) 1 (Sunday) to 7 (Saturday).
Return type: int
usage:dayofweek('2021-11-21')

//加天数
days_add(timestamp startdate, int days)
Return type: timestamp
usage:days_add(now(),2)

//减天数
days_sub(timestamp startdate, int days)
Return type: timestamp
usage:days_sub(now(), 2)

//格式化日期
from_unixtime(bigint unixtime,[string format])
Return type: string

//注意参数
usage:from_unixtime(1392394861,'yyyy-MM-dd');

//得到小时
hour(string date)
Return type: int
usage:hour('2021-11-21 12:32:54')

//增加小时
hours_add(timestamp date, int hours)
Return type: timestamp
usage:hours_add(now(),2)

//减少
hours_sub(timestamp date, int hours)
Return type: timestamp
usage:hours_sub(now(),2)

//得到分钟
minute(string date)
Return type: int
usage:minute(now())

//增加分钟
minutes_add(timestamp date, int minutes)
Return type: timestamp
usage:minutes_add(now(),2)

//减少分钟
minutes_sub(timestamp date, int minutes)
Return type: timestamp
usage:minutes_sub(now(),2)

//得到月份
month(string date)
Return type: int
usage:month('2021-11-21 12:32:54')

//月份相加
months_add(timestamp date, int months)
Return type: timestamp
usage:months_add(now(),3)

//减月份
months_sub(timestamp date, int months)
Return type: timestamp
months_sub(now(),3)

//得到秒
second(string date)
Return type: int

//秒加
seconds_add(timestamp date, int seconds)
Return type: timestamp

//秒减
seconds_sub(timestamp date, int seconds)
Return type: timestamp

//得到日期
to_date(now())

//得到1970到今秒
unix_timestamp(),
unix_timestamp(string datetime),
unix_timestamp(string datetime, string format),
unix_timestamp(timestamp datetime)
Return type: bigint

//得到这周是这年的多少周
weekofyear(string date)
Return type: int
usage:weekofyear('2021-11-21 12:32:54')

//周加
weeks_add(timestamp date, int weeks)
Return type: timestamp
usage:weeks_add('2021-11-21 12:32:54', 1)

//周减
weeks_sub(timestamp date, int weeks)
Return type: timestamp
usage:weeks_sub('2021-11-21 12:32:54', 1)

//得到年
year(string date)
Return type: int

//年加
years_add(timestamp date, int years)
Return type: timestamp

//年减
years_sub(timestamp date, int years)
Return type: timestamp
  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小挽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值