Solved

Help on an Oracle Query

Posted on 2010-11-16
6
308 Views
Last Modified: 2012-06-21
For anyone who knows Oracle this should be cake.  For me being proficient and used to SQL Server this is a complete headache that I can't figure out something that would be so easy.

I've got a table called Customer with a relevant column ExpirationDate

I need a query that will return all the records in this table where the expirationDate is less than the current date.  Or in other words, all records having an expiration data that has already passed.

This is what I have so far...
SELECT * FROM Customer
WHERE EXPIRATIONDATE < SYSDATE

which sort of works, but I need records that have an expiration date of the current date not to show up in the query.

The expiration dates in the table are all in the form of...
16-NOV-10 12.00.00

So because SYSDATE returns a date that has a time that is later in the day than the DateTime above, that record is being included in the query.  I only want a record having an expiration date of 16-Nov to show up in this query if today's date were 17-Nov or later.

Thanks for the help.
0
Comment
Question by:JosephEricDavis
6 Comments
 
LVL 40

Accepted Solution

by:
Sharath earned 450 total points
Comment Utility
try this.


SELECT * FROM Customer
WHERE EXPIRATIONDATE < TRUNC(TO_DATE(SYSDATE),'DD')
0
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
Use TRUNC(SYSDATE)
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 50 total points
Comment Utility
Just noticed the other comment

SYSDATE is already a date, so TO_DATE is superfluous.  Also, the default for TRUNC is to a whole number, which is the day, so 'DD' is not required, so

< TRUNC(SYSDATE)

is all that is required (but the first comment is not wrong)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:paulwquinn
Comment Utility
Use the TRUNC function on SYSDATE to effectively set the time to 0000 hours, i.e. midnight. The inequality comparison in the WHERE clause will then only pick up dates prior to SYSDATE.

SELECT * FROM Customer
WHERE EXPIRATIONDATE < SELECT TRUNC(SYSDATE) FROM DUAL;
0
 
LVL 1

Expert Comment

by:sunny25
Comment Utility
this query should give you the desired result
:-SELECT * FROM Customer
WHERE trunc(EXPIRATIONDATE) < trunc(SYSDATE)
Trunc() function truncates the time part from date and return only the date
0
 
LVL 7

Author Closing Comment

by:JosephEricDavis
Comment Utility
I'm in the habit of giving points to the first guy that has a working post and not splitting them with folks that chime in after with the same answer.  But this one was a hard call as the first answer did work but the second answer was an improvement upon.  Therefore I chose to award points as I have.

Thanks guys.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now