如何从字符串oracle中提取日期格式

时间:2017-04-26 12:06:47

标签: oracle date

我试图找出有多少人的身份证已从数据库中过期,因为数据具有以下格式:

CI+OT+1x78xx+POL.SLATINA+29-08-2003+14-07-2013

BI+GV+591xxx+MUN CAMPIA TURZII+03-02-1997+

所以它被分隔了+五​​次(其中一些不会过期)

我已经这样做了,但它不起作用......

select 
set_act_id_1,
to_number(sysdate-to_date(substr(set_act_id_1,instr(set_act_id_1,'+',1,5)+1,length(set_act_id_1)),'DD-MM-YYYY')) 
from STAGE.CUSTOMERS 
where set_act_id_1 is not null 
group by set_act_id_1 
having sysdate-to_date(substr(set_act_id_1,instr(set_act_id_1,'+',1,5)+1,length(set_act_id_1)),'DD-MM-YYYY')<0;

1 个答案:

答案 0 :(得分:2)

SELECT set_act_id_1,
       TO_DATE( REGEXP_SUBSTR( set_act_id_1, '[^+]+', 1, 6 ), 'DD-MM-YYYY' )
FROM   STAGE.CUSTOMERS 
WHERE  set_act_id_1 is not null

或者

SELECT set_act_id_1,
       TO_DATE( SUBSTR( set_act_id_1, INSTR( set_act_id_1, '+', 1, 5 ) + 1 ), 'DD-MM-YYYY' )
FROM   STAGE.CUSTOMERS 
WHERE  set_act_id_1 is not null

<强>更新

SELECT CASE
         WHEN REGEXP_LIKE( expiry_date, '^\d{1,2}[ \/-](JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[ \/-]\d{4}$', 'i' )
         THEN TO_DATE( expiry_date, 'dd mon yyyy' )
         WHEN REGEXP_LIKE( expiry_date, '^\d{1,2}[ \/-](JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[ \/-]\d{2}$', 'i' )
         THEN TO_DATE( expiry_date, 'dd mon yy' )
         WHEN REGEXP_LIKE( expiry_date, '^(0?[1-9]|[12]\d|3[01])[ \/-](0?[1-9]|1[0-2])[ \/-]\d{4}$' )
         THEN TO_DATE( expiry_date, 'dd mm yyyy' )
         WHEN REGEXP_LIKE( expiry_date, '^(0?[1-9]|1[0-2])[ \/-](0?[1-9]|[12]\d|3[01])[ \/-]\d{4}$' )
         THEN TO_DATE( expiry_date, 'mm dd yyyy' )
         WHEN REGEXP_LIKE( expiry_date, '^(0?[1-9]|[12]\d|3[01])[ \/-](0?[1-9]|1[0-2])[ \/-]\d{2}$' )
         THEN TO_DATE( expiry_date, 'dd mm yy' )
         WHEN REGEXP_LIKE( expiry_date, '^(0?[1-9]|1[0-2])[ \/-](0?[1-9]|[12]\d|3[01])[ \/-]\d{2}$' )
         THEN TO_DATE( expiry_date, 'mm dd yy' )
         ELSE NULL
       END AS expiry_date
FROM   (
  SELECT set_act_id_1,
         SUBSTR( set_act_id_1, INSTR( set_act_id_1, '+', 1, 5 ) + 1 ) As expiry_date
  FROM   STAGE.CUSTOMERS 
  WHERE  set_act_id_1 is not null
)