yyyy-MM-dd與yyyyMMdd000000轉換的三種方法
方法一:date_format(只支持yyyy-MM-dd -> yyyyMMdd000000)
select date_format('2019-10-07', 'yyyyMMdd000000') -- 20191007000000
方法二:from_unixtime + unix_timestamp
select from_unixtime(unix_timestamp('2019-10-07', 'yyyy-MM-dd'), 'yyyyMMdd000000') -- 20191007000000 select from_unixtime(unix_timestamp(substr('20191007000000',1,8),'yyyyMMdd'),'yyyy-MM-dd') -- 2019-10-07
方法三:substr + concat
select concat(substr('2019-10-07',1,4),substr('2019-10-07',6,2),substr('2019-10-07',9,2),'000000') -- 20191007000000 select concat(substr('20191007000000',1,4),'-',substr('20191007000000',5,2),'-',substr('20191007000000',7,2)) -- 2019-10-07
時間轉換方法詳解
unix_timestamp:格式化日期轉時間戳
select unix_timestamp('2019-10-07 13:24:20','yyyy-MM-dd HH:mm:ss') -- 1570425860 select unix_timestamp('20191007','yyyyMMdd') -- 1570377600
from_unixtime:時間戳轉格式化日期
select from_unixtime(1570425860,'yyyy-MM-dd HH:mm:ss') -- 2019-10-07 13:24:20 select from_unixtime(1570425860,'yyyyMMdd000000') -- 20191007000000
date_format:yyyy-MM-dd HH:mm:ss 時間轉格式化時間
select date_format('2019-10-07 13:24:20', 'yyyyMMdd000000') -- 20191007000000 select date_format('2019-10-07', 'yyyyMMdd000000') -- 20191007000000
yyyy-MM-dd HH:mm:ss 注意
MM為月份
mm為分鍾
HH為24小時制
hh為12小時制