Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Selection 1

Status
Not open for further replies.

kskid

Technical User
Mar 21, 2003
1,767
US
Can someone explain why the following works

SELECT *
FROM TABLEA
WHERE TABLEA.DATE = TO_DATE('20060605','YYYYMMDD')

but not this

SELECT *
FROM TABLEA
WHERE TABLEA.DATE = SYSDATE - 1

I'm using Oracle 8i
 
KSKid said:
...but not this...
I cannot clearly see your terminal in Kansas, so please help me out here...Is your problem a syntax error, a run-time error, or a logic-error (the code displays unexpected/unwanted results)?

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 
...and please be sure than when you answer my inquiry that you post the actual outcome that represents your "...but not this..." results.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 
Oracle dates have a time component:
Code:
SQL> select to_char(SYSDATE,'YYYY MON DD HH24:MI:SS') NOW
  2  from DUAL
  3  /

NOW
--------------------
2006 JUN 06 14:26:47

SQL>
thus SYSDATE will not be equal to a stored date that has no time component.

Remove the time element by:
Code:
SELECT *
FROM TABLEA
WHERE TABLEA.DATE = trunc(SYSDATE) - 1

Beware of false knowledge; it is more dangerous than ignorance. ~George Bernard Shaw
Consultant Developer/Analyst Oracle, Forms, Reports & PL/SQL (Windows)
My website: Emu Products Plus
 
That did it.

On the first query, I had 779 rows returned but none with the second. The date field is strictly a date and not datetime so the TRUNC(SYSDATE-1) solved it.

 
Barb,

Great intuition...great catch!

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I can provide you with low-cost, remote Database Administration services: see our website and contact me via www.dasages.com]
 
Thanks. This has to be one of the most frequent questions I have been asked by developers. Now, whenever I see anything compared to SYSDATE, it raises a huge red flag!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top