2

I cannot figure out why this code will not work. It is a bit backwards, which may be confusing me.

SELECT * FROM 
(  
 SELECT   
 TO_CHAR(FTPRITIN.start_date, 'DD-MON-YY') start_date,
 TO_CHAR(FTPRITIN.end_date, 'DD-MON-YY') end_date 
 FROM ftpritin 
)
WHERE :VARIABLE BETWEEN start_date AND end_date

I have also tried

SELECT * FROM 
(  
 SELECT   
 TO_CHAR(FTPRITIN.start_date, 'DD-MON-YY') start_date,
 TO_CHAR(FTPRITIN.end_date, 'DD-MON-YY') end_date 
 FROM ftpritin 
)
WHERE start_date > :VARIABLE AND end_date < :VARIABLE

But it does not seem to work. The query runs but grabs data completely out of the range.

What i need is to grab data rows where the variable falls between the start_date and end_date. Any ideas? Any help is much appreciated.

ANSWER

FYI - For those who wonder why such an easy problem was so hard - Or for those who have a similar problem...

The field was a TIMESTAMP and not a date field. I figured this out by running

DESC table_name

Then i just did a quick substr to_date to fix it

to_date(substr(start_date, 1, 9))

This allowed me to filter by date

WHERE to_date(substr(start_date, 1, 9)) <= to_date(:VARIABLE, 'DD-MON-YY')...

Thanks for the help everyone.

(Less than 10 rep so i cant answer my own post)

1 Answer 1

2

Your date formats are in the wrong order. But, why can't you just do:

SELECT * 
FROM ftpritin 
WHERE start_date > :VARIABLE AND end_date < :VARIABLE

Where :VARIABLE is a date/datetime? Or, if it has to be a string:

SELECT * 
FROM ftpritin 
WHERE start_date > to_date(:VARIABLE, <right format goes here>) AND
      end_date < to_date(:VARIABLE, <right format goes here>)
Sign up to request clarification or add additional context in comments.

5 Comments

Im not a pro at databases, but to_date() wouldn't work on these fields, thats why i used to_char().
If the field is already a date, using to_date() is unnecessary. However, if the variable is a string, using to_date() on it seems like a good idea.
Is this not backwards? I want my variable to be between start_date and end_date. start_date will always be earlier on the calendar than end_date, so wouldn't it be start_date < to_date(var) AND end_date > to_date(var) ?
FYI - For those who wonder why such an easy problem was so hard - Or for those who have a similar problem... The field was a TIMESTAMP and not a date field. I figured this out by running DESC table_name Then i just did a quick substr to_date to fix it to_date(substr(start_date, 1, 9)) This allowed me to filter by date WHERE to_date(substr(start_date, 1, 9)) <= to_date(:VARIABLE, 'DD-MON-YY')... Thanks for the help everyone. (Less than 10 rep so i cant answer my own post)
@CadeWard . . . The logic for the condition was taken from your question.

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.