既然你说你“无权访问”数据库,我假设你不能创建任何函数来帮助你,你只能运行查询?
如果是这种情况,那么以下代码应该可以满足您的大部分需求,但需要注意以下几点:
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 值至少具有正确的格式。我必须打破每个时间单位月/日/年来进行 to_date 评估,因为当我在 sdate 上执行 to_date 时仍然收到无效的月份错误。
我希望这会有所帮助。