法1:from_unixtime+ unix_timestamp/这个试了没成功
–20171205转成2017-12-05
select from_unixtime(unix_timestamp(‘20171205’,‘yyyymmdd’),‘yyyy-mm-dd’) from dual;
–2017-12-05转成20171205
select from_unixtime(unix_timestamp(‘2017-12-05’,‘yyyy-mm-dd’),‘yyyymmdd’) from dual;
法2:substr + concat/这个试了成功了
–20171205转成2017-12-05
select concat(substr(‘20171205’,1,4),’-’,substr(‘20171205’,5,2),’-’,substr(‘20171205’,7,2)) from dual;
–2017-12-05转成20171205
select concat(substr(‘2017-12-05’,1,4),substr(‘2017-12-05’,6,2),substr(‘2017-12-05’,9,2)) from dual;
本文介绍了两种在SQL中进行日期格式转换的方法:使用from_unixtime和unix_timestamp结合,以及利用substr和concat函数。通过示例展示了如何将'20171205'转换为'2017-12-05',以及将'2017-12-05'转换为'20171205',并讨论了这两种方法的适用场景。

425

被折叠的 条评论
为什么被折叠?



