yyyymmdd和yyyy-mm-dd日期之间的切换 方法1: 先转成时间戳,再转成日期字符串,并格式化;from_unixtime + unix_timestamp

-- 20210727 转成 2021-07-27
select from_unixtime(unix_timestamp('20210727','yyyymmdd'),'yyyy-mm-dd') -- 2021-07-27 转成 20210727 select from_unixtime(unix_timestamp('2021-07-27','yyyy-mm-dd'),'yyyymmdd') 1 2 3 4 方法2: substr + concat 操作字符串

-- 20210727 转成 2021-07-27
select concat(substr('20210727',1,4),'-',substr('20210727',5,2),'-',substr('20210727',7,2)) -- 2021-07-27 转成 20210727 select concat(substr('2021-07-27',1,4),substr('2021-07-27',5,2),substr('2021-07-27',7,2)) 1 2 3 4 需要注意的是,时间戳有可能是毫秒级的,然后这时候直接使用from_unixtime(1441565203,‘yyyy/MM/dd HH:mm:ss’) 的话就会得到很奇怪的日期了,这时候要这样 from_unixtime(cast(151331629920/1000 as int)),同样的,时间转成毫秒级的时间戳也要乘以1000,如:unix_timestamp(‘2018-12-18 00:38:50’)*1000 ———————————————— 版权声明:本文为CSDN博主「程序员小陶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:​​https://blog.csdn.net/qq_31975963/article/details/89919610​