简体   繁体   中英

Conversion of varchar to timestamp in oracle 11g R2

I am trying to convert varchar to timestamp with below query.

select staging.revival_date ,
   case when staging.revival_date <> 'Unknown' 
        then  
             TO_TIMESTAMP(
                          to_char(staging.revival_date,'dd-MON-YY') 
                          ||
                          '00:00', 'dd-MM-YY HH24:MI'
                         )  
       else null 
   end 
revival_on 
from stg_avg_gen_plant_outage staging

revival_date : varchar2

But it gives me error like : Invalid number

What is going wrong?

  1. revival_date is a varchar (because sometimes it can apparently contain 'Unknown' so it makes no sense to use to_char() on it.

  2. When you convert to timestamp the format has to match the date stored as a string. In your case you're trying to make something in the format 'dd-MON-YYHH24:MI' (without a space between the date and time components) and telling to_timestamp to expect strings in the format 'dd-MM-YY HH24:MI'. So you need to

    1. Make sure there is indeed a space between the data and time components, and

    2. Make the "month" component match.

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