如何以“ dd / mm / yyyy”格式调整日期

时间:2019-02-07 23:33:29

标签: sql oracle regexp-replace to-date regexp-substr

我正在尝试将日期从“ dd / mm / yyyy”调整为“ dd-mm-yy”,但没有成功。 在我的数据集中,我使用了不同的日期格式,因此虽然可以通过过滤将其正确校正

示例:

  DATES
  -------
  09-MAY-18
  09-NOV-18
  09-OCT-18
  1/2/2017
  1/3/2017
  05/03/2018
  12-OCT-18

因此,日期“ 1/2/2017”,“ 1/3/2017”,“ 05/03/2018”的格式应为“ DD-MM-YYYY”,格式为“ 09-MAY-18”或“ 09-NOV-18”。

我尝试运行的公式是:

   UPDATE TESTE_DATAS SET DATAS = TO_DATE(REGEXP_REPLACE(DATAS, '^\d+\/\d+\/\d+$', 
                                    CONCAT(
                                      CONCAT(
                                        CONCAT(
                                          CONCAT(
                                                REGEXP_SUBSTR(DATAS, '^(\d+)(\/)(\d+)(\/)(\d+)$', 1, 1, 'i', 1),
                                                '-'),
                                                      REGEXP_SUBSTR(DATAS, '^(\d+)(\/)(\d+)(\/)(\d+)$', 1, 1, 'i', 3)),
                                                      '-'),
                                                         REGEXP_SUBSTR(DATAS, '^(\d+)(\/)(\d+)(\/)(\d+)$', 1, 1, 'i', 5)) 
                                        ),'DD-MM-YY') WHERE REGEXP_LIKE(DATAS,'^\d+\/\d+\/\d+$');

最后我得到以下信息:

Error report -
SQL Error: ORA-01843: not a valid month
01843. 00000 -  "not a valid month"
*Cause:    
*Action:

,预期结果应该是:

  DATES
  -------
  09-MAY-18
  09-NOV-18
  09-OCT-18
  01-FEB-17
  01-MAR-17
  05-MAR-18
  12-OCT-18

2 个答案:

答案 0 :(得分:0)

您可以先使用TO_DATE将字符串转换为日期,然后使用TO_CHAR格式化日期:

update teste_datas set datas = to_char(to_date(datas, 'DD/MM/YYYY'), 'DD-MON-YY') where datas like '%/%/%';

答案 1 :(得分:0)

从示例数据来看,字符类型列dates似乎有三种不同的格式。

请考虑以下代码块。如果在转换为'dd-MON-yy'时出现格式化异常,则由于长度

,将区别于其他模型的更新格式
create table teste_datas( id int, dates varchar2(20),derived_date date);
insert all 
       into teste_datas(id,dates) values(1,'09-MAY-18')
       into teste_datas(id,dates) values(2,'1/2/2017')
       into teste_datas(id,dates) values(3,'05/03/2018')
select * from dual;

begin
    for d in ( select id, trim(dates) as dates from teste_datas order by id )
    loop
     begin  
      update teste_datas 
         set derived_date = to_date(d.dates,'dd-MON-yy')
       where id = d.id;
      exception when others then
       begin 
        if length(d.dates)<10 then 
          update teste_datas 
             set derived_date = to_date(d.dates,'mm/dd/yyyy')
           where id = d.id;       
        else   
          update teste_datas 
             set derived_date = to_date(d.dates,'dd/mm/yyyy')
           where id = d.id;       
        end if;   
       end;  
     end;    
    end loop;  
  end;  

  select * from teste_datas;

  ID    DATES       DERIVED_DATE
  ---   ----------  ------------
  1     09-MAY-18   09.05.2018
  2     1/2/2017    02.01.2017
  3     05/03/2018  05.03.2018

  select to_char(derived_date,'dd-MON-yy') as derived_date from teste_datas;

  DERIVED_DATE
  ------------
  09-MAY-18
  02-JAN-17
  05-MAR-18