Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle to_date function. Mask needed

I have string of date from xml file of such kind: '2010-09-09T22:33:44.OZ'

I need to extract only date and time. I want to ignore symbol T and .OZ (time zone). Which mask I should use? Thanks in advance

like image 369
Andrey Khataev Avatar asked Sep 16 '10 07:09

Andrey Khataev


2 Answers

select TO_DATE('2010-09-09T22:33:44.OZ'
              ,'YYYY-MM-DD"T"HH24:MI:SS".OZ"')
from dual;

9/09/2010 10:33:44 PM
like image 106
Jeffrey Kemp Avatar answered Oct 14 '22 06:10

Jeffrey Kemp


If the timezone information is needed:

select to_timestamp_tz('2010-09-09T22:33:44.GMT','YYYY-MM-DD"T"HH24:MI:SS.TZR')
from dual;

09-SEP-10 22.33.44.000000000 GMT

But OZ isn't a recognised timezone abbreviation, so you'd need to do some pre-conversion of that to something that is.

If you want to just ignore that part, and it's fixed, you can do what @Jeffrey Kemp said:

select to_date('2010-09-09T22:33:44.OZ','YYYY-MM-DD"T"HH24:MI:SS."OZ"')
from dual;

09/09/2010 22:33:44 -- assuming your NLS_DATE_FORMAT is DD/MM/YYYY HH24:MI:SS

If you want to ignore it but it isn't fixed then you'll need to trim it off first, something like (using a bind variable here for brevity):

var input varchar2(32);
exec :input := '2010-09-09T22:33:44.OZ';
select to_date(substr(:input,1,instr(:input,'.') - 1),'YYYY-MM-DD"T"HH24:MI:SS')
from dual;

09/09/2010 22:33:44
like image 37
Alex Poole Avatar answered Oct 14 '22 05:10

Alex Poole