hive日期函数
本文主要介绍hive日期函数的用法
1 获取当前日期函数:current_date
语法:current_date()
返回值: string
说明:获取当前日期
举例:
select current_date()
2023-07-09
2 获取当前日期函数:current_timestamp
语法:current_timestamp()
返回值: string
说明:获取当前日期
举例:
select current_timestamp()
2023-07-09 13:41:41
3 获取当前UNIX时间戳函数:unix_timestamp
语法:unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
举例:
select unix_timestamp()
1688910196
4 日期函数UNIX时间戳转日期函数: from_unixtime
语法:from_unixtime(bigint unixtime[, string format])
返回值: string
说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
举例:
select from_unixtime(unix_timestamp(),'yyyyMMdd')
20230709
select from_unixtime(unix_timestamp())
2023-07-09 13:44:21
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
2023-07-09 13:44:21
5 日期转UNIX时间戳函数: unix_timestamp
语法:unix_timestamp(string date)
返回值:bigint
说明: 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0。
举例:
select unix_timestamp('2023-07-09 13:45:03','yyyy-MM-dd HH:mm:ss')
1688910303
6 日期时间转日期函数: to_date
语法: to_date(string timestamp)
返回值: string
说明: 返回日期时间字段中的日期部分。
举例:
select to_date('2023-07-09 13:45:03')
2023-07-09
7 日期转年/月/天/小时/分钟/秒 函数
语法: year/month/day/hour/minute/second(string date)
返回值: int
说明: 返回日期中的年/月/天/小时/分钟/秒。
举例:
select year('2023-07-09 13:45:03') year,
month('2023-07-09 13:45:03') month,
day('2023-07-09 13:45:03') day ,
hour('2023-07-09 13:45:03') hour,
minute('2023-07-09 13:45:03') minute,
second('2023-07-09 13:45:03')second
8 日期转周函数: weekofyear
语法: weekofyear (string date)
返回值: int
说明: 返回日期在当前的周数。
举例:
select weekofyear('2023-07-09 13:45:03')
27
9 查询当月第几天函数: dayofmonth
语法: dayofmonth (string date)
返回值: int
说明: 返回当月第几天。
举例:
select dayofmonth('2023-07-09 13:45:03')
9
10 日期比较函数: datediff
语法: datediff(string enddate, string startdate)
返回值: int
说明: 返回结束日期减去开始日期的天数。
举例:
select datediff(‘2023-09-17’,‘2023-09-15’)
2
11 日期增加函数: date_add
语法: date_add(string startdate, int days)
返回值: string
说明: 返回开始日期startdate增加days天后的日期。
举例:
select date_add('2021-09-17',1)
2021-09-18
select date_add('2021-09-17',-1)
2021-09-16
12 日期减少函数: date_sub
语法: date_sub (string startdate, int days)
返回值: string
说明: 返回开始日期startdate减少days天后的日期。
举例:
select date_sub('2021-09-17',1)
2021-09-16
13 提取周的函数: next_day
语法: next_day(string start_date, string day_of_week)
返回值: string
说明: 返回当前日期对应的下一个周几对应的日期
举例:
SELECT next_day('2023-07-09','MO') Monday,
next_day('2023-07-09','TU') Tuesday,
next_day('2023-07-09','WE') Wednesday,
next_day('2023-07-09','TH') Thursday,
next_day('2023-07-09','FR') Friday,
next_day('2023-07-09','SA') Saturday,
next_day('2023-07-09','SU') Sunday
使用场景:前日期所在周的周一对应的日期和获取当前日期所在周的周日对应的日期
SELECT
date_add(next_day('2023-07-09','MO'),-7),--先获取当前日期的下周一对应的日期,然后减去7天
date_add(next_day('2023-07-09','MO'),-1) --先获取当前日期的下周一对应的日期,然后减去1天
14 返回月末函数: last_day
语法: last_day(string date)
返回值: string
说明: 返回该月的最后一天
举例:
select last_day('2023-07-09')
2023-07-31
15 返回月初函数: trunc
语法: trunc(string date,‘MM’)
返回值: string
说明: 返回该月的第一天
举例:
select trunc(‘2023-07-09’,‘MM’)
2023-07-01
16 日期月份增加函数: add_months
语法: add_months(string startdate,int months)
返回值: string
说明: 返回startdate增加months月数的日期
举例:
select add_months('2023-07-09',-1)
2023-06-09
17 日期格式化函数: date_format
语法: date_format(string date, string format)
返回值: string
说明: 将日期格式化
举例:
select date_format(current_date(),‘yyyy’) year,
date_format(current_date(),‘MM’) month,
date_format(current_date(),‘dd’) day,
date_format(current_date(),‘yyyy-MM’) year_month,
date_format(current_date(),‘yyyy-MM-dd’) year_month_day
18 清洗不规范的日期
注意:将’2023/12/14’ 形式转换为’2023-12-14’ ,才能使用to_date或date_format进行日期形式的转换
with tmp as (
select '2023/12/4' as snapshot_date
union all select '2023/12/14' as snapshot_date
union all select '2024/1/6' as snapshot_date
union all select '2024/1/13' as snapshot_date
)
select date_format(regexp_replace(snapshot_date, '/', '-'),'yyyy-MM-dd') as snapshot_date
from tmp;
with tmp as (
select '2023/12/4' as snapshot_date
union all select '2023/12/14 11:45:23' as snapshot_date
union all select '2024/1/6 12:00:21' as snapshot_date
union all select '2024/1/13' as snapshot_date
)
select date_format(regexp_replace(snapshot_date, '/', '-'),'yyyy-MM-dd HH:mm:ss') as snapshot_date
from tmp;
更多推荐
所有评论(0)