简体   繁体   中英

How to convert string to date format in Oracle

I have a file contains a string like 2011-03-14 11:57:42+08:00 and I need to store it in a column with DATE datatype. I use Oracle 11g.

I have tried TO_DATE function but it didn't work.

Can anyone help me?

thanks in advance

You have to use the function TO_TIMESTAMP_TZ to get the timezone properly parsed:

SELECT CAST(TO_TIMESTAMP_TZ('2011-03-14 11:57:42+08:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') AS DATE) AS my_date 
  FROM DUAL
 ;

(And as a side note to your example in the comment: no need for TO_CHAR , quoting was missing and format model MON (abbreviated month) was incorrect)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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