简体   繁体   中英

Oracle Regular Expression for Date

I have the following anonymous block. I've written a small regular expression to accept the date. It is able to validate the day, month, and year. Now I want to consider the only century year, and regexp should also allow year in RR format (Century year). Also, I want to accept a day without a zero in the prefix; currently, it is accepting this for the month only. Please see the scenarios below:

> 01/12/2154 (Do not accept, It's not a century year)
> 11/12/1996 (Do not accept, It's not a century year)
> 11/2/24 (Accept, and a plsql logic in IF condition could change it to 2024; Add 20 to it)
> 1/9/2020 (Accept)
> 01/02/2020 (Accept)
> 1/29/20 (Accept)
> 01/11/25 (Accept)
> 1/09/2021 (Accept)

**

declare
date_v varchar2(40):= '01/01/2025';
date_n date;
begin
if regexp_like (date_v, '([0-9]|1[0-2])/([0-2][0-9]|3[0-1])/[0-9]{4}') then
-- logic
date_n:= TO_DATE(date_v,'MM/DD/YYYY');
dbms_output.put_line(date_n);
end if;
end;

You can do a regular expression on a date, but that is not a good way to check if a date is valid, it just checks if the format is ok. For example 30-FEB-2020 will pass your regex but that is not a valid date. The database can do the job for you instead. I usually use something like this:

FUNCTION is_valid_date (date_str_i VARCHAR2, format_i VARCHAR2) RETURN VARCHAR2
/* check if date is valid */
AS
  l_dummy_dt DATE;
  date_not_valid_for_m EXCEPTION;
  PRAGMA EXCEPTION_INIT(date_not_valid_for_m, -01839);  
BEGIN
  l_dummy_dt := TO_DATE(date_str_i,format_i);
  RETURN 'Y';
EXCEPTION WHEN date_not_valid_for_m THEN
  RETURN 'N';
END; 

Note that it only fails if the TO_DATE returns ora-01839: date not valid for month specified, you can easily add the other possible errors or just use a WHEN OTHERS THEN RETURN 'N';

Oracle is smart enough to recognize the string with or without 0 in date format.

So you can directly convert this string to TO_DATE('11/2/24', 'mm/dd/rrrr') and for recognizing the current century, You can use the TRUNC to century using the 'CC' as follows:

SQL> SELECT
  2      TO_DATE('11/2/24', 'mm/dd/rrrr') YOUR_DATE,
  3      CASE
  4          WHEN TRUNC(TO_DATE('11/2/24', 'mm/dd/rrrr'), 'CC') = TRUNC(SYSDATE, 'CC') THEN
  5              'ACCEPT'
  6          ELSE
  7              'REJECT'
  8      END RESULT
  9  FROM
 10      DUAL;

YOUR_DATE   RESULT
----------- ------
02-NOV-2024 ACCEPT

SQL>

Result of the query with another date from the past century:

SQL> SELECT
  2      TO_DATE('11/12/1996', 'mm/dd/rrrr') YOUR_DATE,
  3      CASE
  4          WHEN TRUNC(TO_DATE('11/12/1996', 'mm/dd/rrrr'), 'CC') = TRUNC(SYSDATE, 'CC') THEN
  5              'ACCEPT'
  6          ELSE
  7              'REJECT'
  8      END RESULT
  9  FROM
 10      DUAL;

YOUR_DATE   RESULT
----------- ------
12-NOV-1996 REJECT

SQL>

Note : I have used 'rrrr' for a year as it will convert any two-digit years to the year falling in 1950-2049.

Try this one:

with t(date_col) as (
select '01/01/2014' from dual
union all
select '1/2/2014' from dual
union all
select '01/3/2014' from dual
union all
select '1/04/2014' from dual
union all
select '11/1/14' from dual)
select date_col,
       case
         when regexp_instr(date_col, '^\d/\d/\d{4}$') = 1 then
          'd/m/yyyy'
         when regexp_instr(date_col, '^\d{2}/\d/\d{4}$') = 1 then
          'dd/m/yyyy'
         when regexp_instr(date_col, '^\d/\d{2}/\d{4}$') = 1 then
          'd/mm/yyyy'
         when regexp_instr(date_col, '^\d{2}/\d{2}/\d{4}$') = 1 then
          'dd/mm/yyyy'
         else
          'Unknown format'
       end date_format
  from t;

DATE_COL   DATE_FORMAT
---------- --------------
01/01/2014 dd/mm/yyyy
1/2/2014   d/m/yyyy
01/3/2014  dd/m/yyyy
1/04/2014  d/mm/yyyy
11/1/14    Unknown format

Refer to this link:

Regular expression on Dates in Oracle

Date validation with regular expression is difficult, very difficult. THr problem being that months have differing number of days, and 29-Feb is valid only in certain years. Your request is to have multiple valid formats greatly compounding.
The following function validates dates in the ISO-8601 format (YYYY-MM-DD), and only that format, but it does validate the correct number of days in each month for the dates 0001-01-01 through 9999-12-31, including leap days (29-Feb) when valid. but again just 1 format. Recommendation do not try validating dates with regular expression, esp multiple formats.

create or replace function valid_iso_date (iso_date_string varchar2)
   return date
is
   iso_date_pattern constant varchar2(256) := 
       '^((0[0-9]{2}[1-9]|[1-9][0-9]{3})-((0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-8])|(0[469]|11)-(0[1-9]|[12][0-9]|30))$)|^(\d\d(0[48]|[2468][048]|[13579][26])-02-29$)';

   result_date date := null;
begin 
   if regexp_like(iso_date_string, iso_data_pattern)
   then 
      result_date := to_date(iso_date_string,'yyyy-mm-dd');
   end if; 
   return result_date;
end valid_iso_date; 

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