hive中如何将yyyymmdd转成yyyy-mm-dd
时间: 2024-01-12 21:03:21 浏览: 35
在Hive中,可以使用substr、concat和from_unixtime函数将yyyymmdd转换为yyyy-mm-dd格式。例如,假设日期列的名称为date_col,可以使用以下语句进行转换:
```
select concat(substr(date_col, 1, 4), '-', substr(date_col, 5, 2), '-', substr(date_col, 7, 2)) as formatted_date from table_name;
```
其中,substr函数用于提取年、月和日的数字,concat函数用于将它们连接起来形成一个日期字符串,from_unixtime函数可以将字符串解析为日期类型进行操作。
相关问题
hive yyyymmdd转成yyyy-mm-dd
可以使用Hive中的substr函数和concat函数来实现将yyyymmdd转成yyyy-mm-dd的操作。
具体的方法如下:
1. 使用substr函数将原始日期字符串中的年、月、日分别提取出来,例如:
select substr('20220101', 1, 4) as year, substr('20220101', 5, 2) as month, substr('20220101', 7, 2) as day;
这样就可以得到年、月、日分别为2022、01、01的结果。
2. 使用concat函数将年、月、日拼接成新的日期字符串,例如:
select concat(substr('20220101', 1, 4), '-', substr('20220101', 5, 2), '-', substr('20220101', 7, 2)) as new_date;
这样就可以得到新的日期字符串为2022-01-01的结果。
综合起来,可以使用如下的Hive SQL语句将yyyymmdd转成yyyy-mm-dd:
select concat(substr(date_str, 1, 4), '-', substr(date_str, 5, 2), '-', substr(date_str, 7, 2)) as new_date from table_name;
其中,date_str为原始日期字符串,table_name为包含原始日期字符串的表名。
hive 日期函数yyyy-mm-dd转yyyymmdd
在Hive中,可以使用unix_timestamp函数将日期格式从yyyy-mm-dd转换为yyyymmdd的形式。 例如,使用unix_timestamp('2022-01-01', 'yyyy-MM-dd')可以将日期'2022-01-01'转换为时间戳。然后,可以使用from_unixtime函数将时间戳转换为想要的日期格式yyyymmdd。 这样,可以通过组合使用unix_timestamp和from_unixtime函数来实现日期格式的转换。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【HQL实用】时间格式YYYYMMDD转换成YYYY-MM-DD](https://blog.csdn.net/u013631103/article/details/126983750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]