Oracle to_date function. Mask needed

后端 未结 2 549
感情败类
感情败类 2021-01-18 13:54

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). Whi

相关标签:
2条回答
  • 2021-01-18 14:10
    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
    
    0 讨论(0)
  • 2021-01-18 14:20

    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
    
    0 讨论(0)
提交回复
热议问题