2

I have a table with date column in it. I need to fetch the records from it based on the given date.

Currently when i used the query:

select * from workingemployee_data where created_date like '20-Jan-2012'

I am getting those records which have created_date on 20-Jan-2012

But i want to get the records those were created 10 days earlier to a given date (i.e) 20-Jan-2012.

Please suggest me on this.

2
  • 1
    What is the datatype of this column created_date? Commented Feb 3, 2013 at 8:24
  • like does not make any sense for a DATE Commented Feb 3, 2013 at 8:56

3 Answers 3

4

This gives all records between today and 10 days ago:

SELECT *
FROM workingemployee
WHERE created_date BETWEEN sysdate - INTERVAL '10' DAY
                       AND sysdate

This gives all records entered exactly 10 days ago:

SELECT *
FROM workingemployee
WHERE created_date = sysdate - INTERVAL '10' DAY

Replace sysdate with exact date if you want.

Sign up to request clarification or add additional context in comments.

10 Comments

Great that it displays records 10 days before system date. But if i try to replace the sysdate with my custom date like created_date ='25-DEC-2011' - INTERVAL '10' DAY it says invalid datatype for date/time interval.
It seems like your created_date is not really of DATE type, but VARCHAR. This is really bad idea, especially for speed and indexing - please convert this to real DATE. But, you can use this expression as workaround: to_date('25-DEC-2012', 'dd-mon-yyyy')
created_date is of type DATE in my table
ah, ok. still use something like to_date('25-DEC-2012', 'dd-mon-yyyy') to convert your input string into real date type
Glad it helped you! If you have index on created_date, it should work very fast
|
4
  1. Why do you use like and not = ?
  2. Assuming that created_date is of type DATE, it's bad practice to rely on implicit conversion according to NLS_DATE_FORMAT (this is what happens when you compare a date and a string)
  3. dd-mon-yyyy isn't a good format for querying since it deffers according to NLS_LANGUAGE better use mm for months numbers

So, either use @mvp's answer or do something like this:

SELECT *
FROM workingemployee
WHERE trunc(created_date) = to_date('20-01-2013', 'dd-mm-yyyy') - 10

2 Comments

Unfortunately, this solution cannot make use of possible index on created_date, thus it will be using full table scan - very slow, especially if table is big.
This can be workedaround by either using function based indexes or by using create_date >= to_date('20-01-2013 000000', 'dd-mm-yyyy hh24miss') - 10 and create_date >= to_date('20-01-2013 235959', 'dd-mm-yyyy hh24miss') - 10
1

SELECT * FROM workingemployee WHERE created_date > sysdate - INTERVAL '10' DAY;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.