1

I am getting correct result when I query from Microsoft Query but when I query from SQLPLUS(Oracle) I get incorrect result.

Here is Microsoft Query (Result shows correct from June 12th 3 PM to June 13 12 AM.

SELECT 
      NAME,
      CIM,
      NUM_of_People,
      STARTDATETIME
From Table
WHERE (SUSTAINED_FLAG=1) 
AND (PLANNED_FLAG=0) 
AND (STARTDATETIME>{ts '2013-06-12 04:00:00'} 
And STARTDATETIME<{ts '2013-06-13 12:00:00'})



In Oracle I am using this clause which gives me wrong result.
it is showing query result from dates only after June 13 12:AM to 12 PM.

SELECT 
        NAME,
        CIM,
        NUM_of_People,
        TO_CHAR(STARTDATETIME,'MM/DD/YYYY HH:MI:SS') As STARTDATETIME
FROM OBVWH.  
where  BETWEEN To_Date(to_char(STARTDATETIME, 'DD-MON-YYYY')) >= To_Date('06/12/2013 16:00:00','MM/DD/YYYY HH24:MI:SS') 
and To_Date(to_char(STARTDATETIME, 'DD-MON-YYYY')) <= To_Date('06/13/2013 12:00:00','MM/DD/YYYY HH24:MI:SS') 
and sustained_flag = 1 and planned_flag = 0 ORDER BY STARTDATETIME ASC
4

1 回答 1

4

你遇到这个问题的原因是因为在 where 子句中

To_Date(to_char(STARTDATETIME, 'DD-MON-YYYY'))

这样做是从您的日期中剥离时间元素,以便它们都有效地在午夜。一个简单的例子是

SELECT CASE
       WHEN to_date(to_char(SYSDATE, 'DD-MON-YYYY')) = SYSDATE THEN
        'yes'
       ELSE
        'no'
   END
FROM   dual

如您所见, sysdate 在两次更改其格式后不再等于其自身。

由于该列是一个日期,您根本不需要对其进行任何格式化,只需将您的 where 子句更改为

where  STARTDATETIME >= To_Date('06/12/2013 16:00:00','MM/DD/YYYY HH24:MI:SS') 
and    STARTDATETIME <= To_Date('06/13/2013 12:00:00','MM/DD/YYYY HH24:MI:SS') 
and sustained_flag = 1 and planned_flag = 0 ORDER BY STARTDATETIME ASC
于 2013-06-26T13:31:46.860 回答