Solved

Help on an Oracle Query

Posted on 2010-11-16
6
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 41

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

724 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