SparkSQL中的一些函数

1时间相关的函数:

1.1地区相关的函数

以北京时间东八区(UTC+8)为例,需要将UTC时间转换成北京时间,可以使用以下两个函数

to_utc_timestamp(timestamp, timezone) --将timestamp转换成UTC时间

from_utc_timestamp(timestamp, timezone) --UTC时间转换成timezone时间

select   from_utc_timestamp(current_timestamp(), 'GMT')   

结果  

2022-04-29 15:03:29.969

select   from_utc_timestamp("2022-04-29T15:03:29.969Z", 'GMT') 

结果 

2022-04-29 23:03:29.969

2.计算每一周的 周开始 和 周结束 时间

NEXT_DAY(date,char)  
date参数为日期型, char:为1~7或Monday/Mon~Sunday/ 
示例说明:
计算2022-01-05这天所在周的周开始时间:
select  date_sub(next_day('2022-01-05','monday'),7)
2022-01-03
计算2022-01-05这天所在周的周结束时间:
select
case when day_of_week('2022-01-05') = 7 then'2022-01-05' 
else next_day('2022-01-05', 'sunday') end

结果是这天所在周的周结束时间

 

posted @ 2022-04-29 15:11  羽毛球打的贼好  阅读(313)  评论(0)    收藏  举报