在查询的where子句中,我需要检查2天之间的差异是否为> 7天。我尝试了以下内容并尝试了datediff,dateadd,trunc,tochar但每次都会遇到不同的错误。 任何帮助都是适用的。 接下来我得到了ORA-01858:找到了一个非数字字符,其中有一个数字是预期的:
date1-date2> 7
发布查询:
SELECT *
FROM frm_rg_ps_rg ps_rg
WHERE
((SELECT COUNT(*)
FROM frm_rg_ps_rg b
WHERE ps_rg.patient_id = b.patient_id
AND ps_rg.formdata_seq = b.formdata_seq
AND ps_rg.psdate IS NOT NULL
AND b.psdate IS NOT NULL
AND ps_rg.psresult IS NOT NULL
AND b.psresult IS NOT NULL) = 1)
OR NOT EXISTS
(SELECT *
FROM frm_rg_ps_rg c
WHERE (ps_rg.psdate IS NOT NULL AND c.psdate IS NOT NULL AND ps_rg.psresult IS NOT NULL AND c.psresult IS NOT NULL)
AND ps_rg.patient_id = c.patient_id
AND ps_rg.formdata_seq = c.formdata_seq
AND ps_rg.elemdata_seq!=c.elemdata_seq
AND ps_rg.psresult != c.psresult
--AND to_char(ps_rg.psdate ,'yyyy-mm-dd') - to_char(c.psdate ,'yyyy-mm-dd') > 7
--AND Trunc(ps_rg.psdate) > Trunc(c.psdate)+7
AND ps_rg.psdate - c.psdate > 7
)
AND ps_rg.psresult IS NOT NULL
AND ps_rg.psdate IS NOT NULL;
create table frm_rg_ps_rg
(
PATIENT_ID NUMBER
,STUDY_ID NUMBER
,PROCSTEP_ID NUMBER
,FORMDATA_SEQ NUMBER
,ELEMDATA_SEQ NUMBER
,PSUNITOS VARCHAR2(3950 CHAR)
,PSDATE DATE
,PSND VARCHAR2(1 CHAR)
,PSRESULT NUMBER
,PSUNITS VARCHAR2(120)
);
答案 0 :(得分:1)
假设您的字符串列以'yyyy-mm-dd'格式保存日期(如果需要,可以更改)
to_date(date1,'yyyy-mm-dd') - to_date(date2,'yyyy-mm-dd') > 7
OP的演示
create table t (dt1 date,dt2 date);
insert into t (dt1,dt2) values (sysdate,sysdate-10);
select * from t where (select dt1-dt2 from dual) > 7;
+---------------------+---------------------+
| DT1 | DT2 |
+---------------------+---------------------+
| 2016-12-14 10:59:34 | 2016-12-04 10:59:34 |
+---------------------+---------------------+
select * from t where dt1-dt2 > 7;
+---------------------+---------------------+
| DT1 | DT2 |
+---------------------+---------------------+
| 2016-12-14 10:59:34 | 2016-12-04 10:59:34 |
+---------------------+---------------------+
答案 1 :(得分:1)
我可以想到用这个语句来解决这个错误的方法:
date1 - date2 > 7
两个都是约会。如果行中有隐藏字符,即>
和7
之间或7
之后的隐藏字符,则可能会发生这种情况。
您可以尝试删除该行并重新输入,以查看错误是否消失。
答案 2 :(得分:0)
此问题现已解决: 我试过这个: AND(从双重选择(ps_rg.psdate - c.psdate))> 7