0


Hive字符串转日期细节

日期函数:to_date();date_format();from_unixtime(unix_timestamp())

测试数据:t1 t2

2020/4/1 13:01 2020-04-01

目的:将t1转成 2020-04-01 t2 -> 2020/04/01

方法(针对t1进行处理)

from_unixtime(unix_timestamp(substr(t1,1,length(register_time) - 5) ,'yyyy/MM/dd'),'yyyy-MM-dd') -> 2020-04-01

方法(针对t2进行处理)

date_format(t2,'yyyy/MM/dd') -> 2020/04/01

代码案例

1、select date_format('2021-10-22 17:34','yyyy/MM/dd');

2、select from_unixtime(unix_timestamp('2020/4/1','yyyy/MM/dd'),'yyyy-MM-dd');

3、select from_unixtime(unix_timestamp(substr(t1,1,length(t1) - 5) ,'yyyy/MM/dd'),'yyyy-MM-dd') ;

观察:如果日期格式中有 ‘/’ 这样的格式,转换稍微麻烦一些,所以数据源的日期格式尽量以 ‘-’连接,这样就可以直接使用日期转换函数,如to_date(),date_format(),而不必使用字符串切割函数和日期格式匹配函数。

一点思考,如有需要改进之处,请多多指教。

标签: hive

本文转载自: https://blog.csdn.net/m0_70131998/article/details/128853971
版权归原作者 {BOOLEAN} 所有, 如有侵权,请联系我们删除。

“Hive字符串转日期细节”的评论:

还没有评论