0

I have the below query which is giving correct output when the t2.date is not null

select t1.name,t1.work,t2.type from t1,t2 where t.name=t2.name and t1.postdate < t2.date

But when the t2.date is null the query is not fetching any rows. So I want to add the t1.postdate < t2.datecondition only when the t2.date is not null.

When the t2.date is null I don't want to add it to the query(I want the output as the above query gives without and condition)

select t1.name,t1.work,t2.type from t1,t2 where t.name=t2.name 

Please help me with this. Thanks in advance.

2
  • 7
    and (t1.postdate < t2.date OR t2.date is NULL)? Commented Dec 11, 2015 at 9:04
  • So what if I find both a record with a matching date and one with date null for a name? Do you want to join both? Or only the one with the date? With SQL questions it is always advisable to show some sample data and expected result. You are using an out-dated join syntax by the way. Commented Dec 11, 2015 at 9:16

2 Answers 2

3

I agree to @jarlh, you can use this maybe;

select t1.name,t1.work,t2.type from t1,t2 where t.name=t2.name and (t2.date is null or t1.postdate < t2.date)
Sign up to request clarification or add additional context in comments.

Comments

0

Why not uise NVL to make it clearer and shorter?

select t1.name
      ,t1.work
      ,t2.type 
  from t1 
      ,t2 
 where t.name = t2.name 
   and t1.postdate < nvl(t2.date, t1.postdate + 1);

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.