데이터베이스에 "액세스 할 수 없다"고 했으므로이 기능을 지원하는 기능을 만들 수없고 쿼리 만 실행할 수 있다고 가정합니다.
그런 경우 다음 코드는 다음주의 사항과 함께 필요한 것을 최대한 활용해야합니다. 1) 평가할 저장 날짜 형식은 'mm/dd/yyyy'입니다. 그렇지 않은 경우 형식에 맞게 코드를 변경할 수 있습니다. 2) 데이터베이스에 2 월 30 일과 같은 잘못된 날짜가 포함되어 있지 않습니다.이제
create table test (x number, sdate varchar2(20));
insert into test values (1, null);
insert into test values (2, '01/01/1999');
insert into test values (3, '1999/01/01');
insert into test values (4, '01-01-1999');
insert into test values (5, '01/01-1999');
insert into test values (6, '01-01/1999');
insert into test values (7, '12/31/1999');
insert into test values (8, '31/12/1999');
commit;
쿼리 :
WITH dates AS (
SELECT x
, sdate
, substr(sdate,1,2) as mm
, substr(sdate,4,2) as dd
, substr(sdate,7,4) as yyyy
FROM test
WHERE (substr(sdate,1,2) IS NOT NAN -- make sure the first 2 characters are digits
AND to_number(substr(sdate,1,2)) between 1 and 12 -- and are between 0 and 12
AND substr(sdate,3,1) = '/' -- make sure the next character is a '/'
AND substr(sdate,4,2) IS NOT NAN -- make sure the next 2 are digits
AND to_number(substr(sdate,4,2)) between 1 and 31 -- and are between 0 and 31
AND substr(sdate,6,1) = '/' -- make sure the next character is a '/'
AND substr(sdate,7,4) IS NOT NAN -- make sure the next 4 are digits
AND to_number(substr(sdate,7,4)) between 1 and 9999 -- and are between 1 and 9999
)
)
SELECT x, sdate
FROM dates
WHERE to_date(mm||'/'||dd||'/'||yyyy,'mm/dd/yyyy') <= to_date('08/01/1999','mm/dd/yyyy');
그리고 내 결과 :
가
X SDATE
- ----------
2 01/01/1999
WITH 문이 할 것
먼저, 내 테스트 테이블 및 테스트 데이터를 생성 대부분의 유효성 확인은 sdate 값이 최소한 적절한 형식인지 확인해야합니다. 나는 toddate를 수행 할 때 여전히 유효하지 않은 월 오류가 발생했기 때문에 to_date 평가를 수행 할 때마다 월/일/년 단위로 나누어야했습니다.
이 정보가 도움이되기를 바랍니다.
DATE 열에 날짜를 저장하려면 _supposed_하십시오. 너 왜 그래? –
내가 일하는이 레거시 응용 프로그램입니다. 그들은 문자열을 사용합니다. 그렇지 않으면 날짜가 현재 날짜로 기본 설정되어 있기 때문에 ... DB에 대한 제어권이 없습니다 – ntsue
varchar2 필드에 날짜가 있고 일관된 형식이 없어도 SOL (모든 다양한 문자 형식을 실제 날짜 필드로 변환하기위한 테이블 재구성이 부족합니다.) – tbone