presto和hive时间格式转换

1、北京时间格式   to   unix时间格式

数据格式:

2017-11-17 08:28:13

2017-11-17 08:28:10

2017-11-17 08:27:51.343

2017-11-17 08:27:48.021

presto单个标准时间转化(10unix):

select to_unixtime(cast ('2017-08-30 10:36:15' as timestamp))   

 

hive单个标准时间转化(10unix):

select unix_timestamp(cast ('2017-08-30 10:36:15' as timestamp))

presto单个毫秒时间转化(13unix):

select to_unixtime(cast ('2017-12-01 16:42:08.771' as timestamp))

 

hive单个毫秒时间转化(自动转为10unix):

select unix_timestamp(cast ('2017-12-01 16:42:08.771' as timestamp))

presto变量转化(根据时间格式分别转化为10unix13unix):

select to_unixtime(cast (time as timestamp))  

from table1

 

hive变量转化(只能自动转化为10unix):

select unix_timestamp(cast (time as timestamp))   

from table1

where d = '2017-12-05'

2unix时间格式   to  北京时间格式

数据格式:

1510894478

1510855235

1510855290929

1510873252377

presto单个10unix(变为标准格式):

select format_datetime(from_unixtime(1510284058),'yyyy-MM-dd HH:mm:ss')  

 

hive单个10unix(变为标准格式):

select from_unixtime(1323308943123,'yyyy-MM-dd HH:mm:ss')

presto单个13unix(毫秒格式):

select format_datetime(from_unixtime(1510284058.415),'yyyy-MM-dd HH:mm:ss.mmm')

 

hive单个13unix(毫秒格式):手动截取10

select from_unixtime(cast(substr(1323308943123,1,10) as int),'yyyy-MM-dd HH:mm:ss')

Presto  1310unix变量转化(只能截取成10位转化成标准格式):

select format_datetime(from_unixtime(cast(substr(ts,1,10) as double)),'yyyy-MM-dd HH:mm:ss')  from table1

 

hive变量转化(事先截取10unix):

select from_unixtime(cast(substr(ts,1,10) as int),'yyyy-MM-dd HH:mm:ss')

from table1

where d = '2017-12-05'

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Presto中,可以使用`format_datetime`函数将日期格式转换为指定的格式。例如,使用以下语法将Unix时间戳转换为标准的日期时间格式: ``` SELECT format_datetime(from_unixtime(1610085216),'yyyy-MM-dd HH:mm:ss') ``` 这将输出一个`varchar`类型的日期,例如`2021-01-08 13:53:36`。\[1\] 如果要将字符串日期格式转换为标准日期格式并进行加减操作,可以使用`date_parse`函数将字符串解析为日期,然后使用`date_add`函数进行加减操作。例如,以下是一个示例: ``` SELECT date_add('day', -6, cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date)) ``` 这将将字符串日期`20200110`转换为标准日期格式,并在此基础上减去6天。请注意,第三个参数必须转换为`date`格式,否则会报错。\[2\] 另外,如果要将日期转换为指定的格式,可以使用`format_datetime`函数。例如: ``` SELECT format_datetime(cast('2021-06-19' as date),'yyyy-MM-dd') ``` 这将将日期`2021-06-19`转换为`varchar`类型的日期,例如`2021-06-19`。\[3\] #### 引用[.reference_title] - *1* *3* [hive&presto日期与字符串转换](https://blog.csdn.net/lzxlfly/article/details/108532265)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [presto时间转换、时间加减、时间差](https://blog.csdn.net/qq_21997625/article/details/115665541)[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^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值