简体   繁体   English

使用to_char函数在sql中转换日期

[英]convert date in sql using to_char function

I would like to convert the following date to this format(DD-MON-YYYY). 我想将以下日期转换为此格式(DD-MON-YYYY)。

I tried executing the below query but I got the error saying "date format not recognised". 我尝试执行以下查询,但收到错误消息“无法识别日期格式”。

select to_char(to_date('Sat Dec 01 00:00:00 IST 2012','EEE Mon dd HH:mm:ss z yyyy'),'DD-MON-YYYY') from dual;

For everything but the time zone: 对于除时区以外的所有内容:

'DY MON DD HH24:MI:SS YYYY'

For timezone support you need to use a conversion function that supports a timezone like TO_TIMESTAMP_TZ() and have the time zone name as one Oracle recognizes in the form it recognizes. 为了支持时区,您需要使用支持时区的转换函数,例如TO_TIMESTAMP_TZ(),并以Oracle识别的形式将时区名称作为一个Oracle识别。

select to_char( TO_TIMESTAMP_TZ( 
        REPLACE( 'Sat Dec 01 21:00:00 IST 2012','IST','Asia/Calcutta'),
       'DY MON DD HH24:MI:SS TZR YYYY'),'DD-MON-YYYY') from dual;

The relation between time zone names and abbreviations is one to many for most. 时区名称和缩写之间的关系对于大多数人是一对多的。

SELECT tzname, tzabbrev FROM v$timezone_names where TZABBREV = 'IST'

For your example it would probably be easier to remove some or all of the date parts you don't need in the output before conversion. 以您的示例为例,转换前在输出中删除不需要的某些或所有日期部分可能会更容易。

select to_char( to_date( replace('Sat Dec 01 21:00:00 IST 2012','IST',''),
       'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY') from dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM