2014-09-23 75 views
0

如果我執行查詢Oracle查詢沒有返回預期的記錄

select * from my_table where status=5000 

我從數據庫中獲取76條記錄。不過,我想按日期縮小範圍。幸運的是,該表中有一個receiveddate列,其中一個記錄的receiveddate爲「23-APR-13」。所以我試過這個查詢:

select * 
    from my_table 
where status=5000 
    and receiveddate=to_date('23-APR-13', 'DD-Mon-YY') 

並且返回0條記錄。

如果我使用

select * 
    from my_table 
where status=5000 
    and receiveddate > to_date('20130423', 'YYYYMMDD') 
    and receiveddate > to_date('20130424', 'YYYYMMDD') 

我得到正是我想要的。你們可以幫我理解差異/問題嗎?

+1

'Date'數據類型的值總是包含時間部分。嘗試截斷它,'trunc(receiveddate)= to_date('23 -APR-13','DD-Mon-YY')' – 2014-09-23 19:31:44

+0

我假設返回您期望的數據的查詢實際上是'receiveddate 2014-09-23 19:38:00

回答

4

嘗試

select * 
    from my_table 
where status=5000 
    and trunc(receiveddate)=to_date('23-APR-13', 'DD-Mon-YY') 

TRUNC將返回長日期,其中包括時間還的日期部分。

+2

只要記住在使用'where trunc(date_column)= mydate'時要小心。如果您希望Oracle在date_column上使用索引,則不會發生,而是可能進行全表掃描(取決於其他可用索引)。在這種情況下,您可能需要基於函數的'trunc(date_column)'索引或而是使用'where date_column> = mydate和date_column 2014-09-24 11:18:09