日期格式和结果表格过去24小时

时间:2015-08-22 14:29:33

标签: sql oracle date-formatting to-date ora-01843

我是SQL的新手,我遇到了下面的查询问题。我需要返回在过去24小时内输入的订单会话,如果我没有最后一个语句,查询将返回所有订单会话,但我只需要过去24小时内的那些。 CCDBA.O_PAT.ORDER_DDT不是Oracle Date需要使用ddt.tochar转换为可读日期的数字。

-2,147,483,648 and 2,147,483,647 (inclusive).

我得到了以下错误:

int

来自`from distutils.core import setup import py2exe # setup.py # class Target: def __init__(self, **kw): self.__dict__.update(kw) # for the versioninfo resources self.version = "0.5.0" self.company_name = "Company" self.copyright = "no copyright" self.name = "Test22" myservice = Target( description = 'Edit Logon Service', modules = ['Logon_Service'], cmdline_style='pywin32' ) ` 的原始数据如下所示:SELECT DISTINCT CCDBA.O_PAT.SESSION_ID, CCDBA.PATIENT.MEDREC_ID "MRN", CCDBA.PATIENT.VISIT_NUMBER "Account Number", CCDBA.PATIENT.LAST_NAME || ', ' || CCDBA.PATIENT.FIRST_NAME "Patient", CCDBA.PATIENT.DEPT_ID "Floor", CCDBA.PATIENT.ROOM_ID "Room", ddt.tochar(CCDBA.O_PAT.ORDER_DDT) "Order Date" FROM CCDBA.PATIENT INNER JOIN CCDBA.O_PAT ON CCDBA.O_PAT.PAT_SEQ = CCDBA.PATIENT.PAT_SEQ WHERE CCDBA.O_PAT.ORDER_ID = '681278' AND TO_DATE(ddt.tochar(CCDBA.O_PAT.ORDER_DDT), 'DD-MON-YY HH24:MI:SS') >= SYSDATE -1; 使用ddt.tochar进行转换后来自ORA-01843: not a valid month 01843. 00000 - "not a valid month" *Cause: *Action: 的外观数据:CCDBA.O_PAT.ORDER_DDT

1 个答案:

答案 0 :(得分:0)

您无需将日期转换为字符串,然后再返回以比较日期。简化条件:

WHERE CCDBA.O_PAT.ORDER_ID = '681278' AND
      CCDBA.O_PAT.ORDER_DDT >= SYSDATE - 1;

此外,如果您正在学习SQL,请学习使用表别名。生成的查询更易于编写和阅读:

SELECT DISTINCT o.SESSION_ID, 
       p.MEDREC_ID  as "MRN",
       p.VISIT_NUMBER as "Account Number",
       p.LAST_NAME || ', ' || p.FIRST_NAME as "Patient",
       p.DEPT_ID as "Floor",
       p.ROOM_ID as "Room",
       ddt.tochar(o.ORDER_DDT) as "Order Date"
FROM CCDBA.PATIENT p INNER JOIN
     CCDBA.O_PAT o
     ON o.PAT_SEQ = p.PAT_SEQ
WHERE o.ORDER_ID = '681278' AND c.ORDER_DDT >= SYSDATE - 1;

而且,如果您不需要DISTINCT,请不要使用它。它只是在不需要时浪费处理时间。

编辑:

修改日期格式:

WHERE CCDBA.O_PAT.ORDER_ID = '681278' AND
      TO_DATE(ddt.tochar(CCDBA.O_PAT.ORDER_DDT),
              'MM/DD/YYYY HH24:MI') >= sysdate - 1;