在hive执行select from_unixtime(cast('1679546406051' as bigint),'yyyyMMdd')的结果是551920915,不是我想要的年月日,怎么样能得到正确的年月日
时间: 2024-05-10 07:21:04 浏览: 22
在执行select from_unixtime(cast('1679546406051' as bigint),'yyyyMMdd')时,结果为551920915,这是因为时间戳的单位是毫秒,而from_unixtime函数的参数是秒数。因此,正确的做法是将毫秒转换为秒,然后再进行格式化。可以使用以下语句来得到正确的年月日:
```
select from_unixtime(cast('1679546406.051' as double),'yyyyMMdd');
```
结果为:20220722
注意:在将毫秒转换为秒时,需要将毫秒数除以1000得到秒数,并将结果转换为double类型。
相关问题
hive中怎么from_unixtime使用方法
在Hive中,from_unixtime函数的作用是将Unix时间戳转换为对应的日期和时间格式。它的语法如下:
```
from_unixtime(unixtime[, format])
```
其中,`unixtime`参数是Unix时间戳,以秒为单位;`format`参数是可选的,用于指定输出的日期和时间格式,默认为`yyyy-MM-dd HH:mm:ss`。
例如,要将Unix时间戳`1623204398`转换为日期和时间格式,可以使用以下语句:
```
SELECT from_unixtime(1623204398);
```
输出结果为:
```
2021-06-09 11:26:38
```
如果要指定输出格式,可以使用以下语句:
```
SELECT from_unixtime(1623204398, 'yyyy/MM/dd HH:mm:ss');
```
输出结果为:
```
2021/06/09 11:26:38
```
需要注意的是,Hive中的Unix时间戳是以UTC时区为基准的,因此在使用from_unixtime函数时需要格外注意时区的问题。
hive中利用 from_unixtime(unix_timestamp())获取前一天的日期
可以使用Hive中的date_sub函数来获取前一天的日期,结合from_unixtime和unix_timestamp函数可以实现。具体方法如下所示:
```hive
select from_unixtime(unix_timestamp(date_sub(from_unixtime(cast(1683701579457/1000 as int)),1)), 'yyyy-MM-dd');
```
其中,先将时间戳转换为日期格式,再使用date_sub函数获取前一天的日期,最后再将日期转换为时间戳格式并格式化输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)