Solved

Help on an Oracle Query

Posted on 2010-11-16
6
309 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
ID: 34150145
try this.


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

Expert Comment

by:cyberkiwi
ID: 34150146
Use TRUNC(SYSDATE)
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 50 total points
ID: 34150167
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Expert Comment

by:paulwquinn
ID: 34150219
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
ID: 34152907
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
ID: 34155855
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Slow SQL query 12 55
Error when loading the database 16 57
sort a spool into file output in oracle 1 22
'G_F01' is not a procedure or is undefined 3 12
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

867 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

22 Experts available now in Live!

Get 1:1 Help Now