将yyyy / mm / dd转换为dd / mm / yyyy hh24:MI:SS

时间:2016-06-28 11:19:28

标签: sql oracle

我尝试将格式为2016/06/26的数据转换为26/06/2016 00:00:00 我一直在尝试几个选项,一直收到错误"无效的月份名称", 有什么想法/建议吗? 感谢

select to_date('2016/05/07 00:00:00','mm/dd/yyyy HH24:MI:SS') from dual

1 个答案:

答案 0 :(得分:1)

要将字符串转换为日期,您需要先将其转换为日期。您的问题是您尝试格式化字符串而不是日期。所以对于你的具体情况,它将是:

--convert it first to a date
select to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS') 
  from dual

--then convert it to a string in the format you want:
select to_char( to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS'),
                'mm/dd/yyyy HH24:MI:SS' )
  from dual

--since you want it as a date:

--then convert it to a string in the format you want:
select to_date( to_char( to_date('2016/05/07 00:00:00',
                                 'yyyy/mm/dd HH24:MI:SS'),
                         'mm/dd/yyyy HH24:MI:SS' )
                'mm/dd/yyyy HH24:MI:SS' ) 
  from dual

如果您只想将字符串转换为日期,无论格式如何,只需使用我显示的第一个选择。感谢@Boneist在评论中指出它。