Riddhi Valecha

Riddhi Valecha

  • 438
  • 3.3k
  • 415.4k

If condition in Where Clause in SQL Query(Please help)

Nov 26 2013 1:43 AM
Hi all...
I have a table like below in the database.
ID  Username Date1 Date2 
 101 abc 2012-11-01NULL 
 102 def 2012-12-032013-01-05 
 103 ghi 2011-02-042012-03-04 
 104 jkl 2012-02-03 2013-01-01 
 105 mno NULLNULL 
 106 pqr NULL2013-04-01 

Now, I want to use the "IF-Condition" in my sql query such that -

select * from table where Date1<Date2 and Date1 is not null and Date2 is not null, and Total Days between Date1 and Date2.

The result i want is -

ID  Username Date1 
Date2  Total Datys
 101 abc 2012-11-01
NULL  Null
 102 def 2012-12-03
2013-01-05  10
 103 ghi 2011-02-04
2012-03-04  10
 104 jkl 2012-02-03 
2013-01-01  0 (Date1> Date2)
 105 mno NULL
NULL  Null
 106 pqr NULL
2013-04-01  Null


How do I get this?? Please help...

Thanks a ton in advance....

Answers (8)