I have a table like below in the database.
| ID | Username | Date1 | Date2 |
| 101 | abc | 2012-11-01 | NULL |
| 102 | def | 2012-12-03 | 2013-01-05 |
| 103 | ghi | 2011-02-04 | 2012-03-04 |
| 104 | jkl | 2012-02-03 | 2013-01-01 |
| 105 | mno | NULL | NULL |
| 106 | pqr | NULL | 2013-04-01 |
Now, I want to use the "IF-Condition" in my sql query such that -
select * from table where Date1
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....
Jignesh TrivediPosted Mar 21, 2014, 6:08 AM
hi,
try
create table #temp
(
ID int,
Username varchar(20),
Status varchar(10),
Date1 date,
Date2 date,
date3 date
)
insert into #temp values(101,'abc','Clear','2012-11-01','2013-01-01','2013-01-01'),
(102,'def','Rejected','2012-12-03','2013-01-05','2013-01-01'),
(103,'ghi','Clear','2011-02-04','2012-03-04','2013-01-01'),
(104,'jkl','Rejected','2012-02-03','2013-01-01','2013-01-01'),
(105,'mno','Rejected','2013-01-01','2013-01-01','2013-01-01'),
(106,'pqr','Clear','2013-01-01','2013-04-01','2013-01-01')
drop table #temp
select Date1,Date2,Date3,Status,Username,
case Status when 'Clear' then datediff(DAY,Date2,Date1)
else datediff(DAY,Date3,Date2) end as TotalDays
from #temp
hope this will help you.
Riddhi ValechaPosted Mar 21, 2014, 3:18 AM
I have 3 dates columns (Date1, Date2, Date3) , 1 status column , 1 ID and 1 username column.
THere are 2 status - Clear and Reject.
my result -
If status is Clear, then Total number of days between Date2-Date1.
If status is Rejected, then Total number of days between Date3-Date2.
Jignesh TrivediPosted Mar 20, 2014, 6:30 AM
I am not much clear about the quesition
but using case when you can write If condition in where clause
like
create table #temp
(
ID int,
Username varchar(20),
Date1 date,
Date2 date
)
insert into #temp values(101,'abc','2012-11-01',NULL),
(102,'def','2012-12-03','2013-01-05'),
(103,'ghi','2011-02-04','2012-03-04'),
(104,'jkl','2012-02-03','2013-01-01'),
(105,'mno',NULL,NULL),
(106,'pqr',NULL,'2013-04-01')
select * from #temp where case when (Date1 is not null and Date2 is not null) then datediff(DAY,Date1,Date2) else 0 end >=0
Pradip PandeyPosted Mar 20, 2014, 4:26 AM
Use this select statement
Hope it will help you.
venkata kumarPosted Mar 20, 2014, 3:53 AM
Riddhi ValechaPosted Mar 20, 2014, 2:47 AM
I want this date difference on basis of a third column - Status.
My requirement is -
Here in "TotalDays", I want total number of days between 2 dates.
When status is Clear, then Date2-Date1== say xdays.
When status is Rejected, then Date3-Date3-Date2==say ydays.
Please help
venkata kumarPosted Nov 26, 2013, 4:54 AM
try the fallowing
select id,useername,date1,date2,DATEDIFF(day, Date1, date1) as total from tablename
Girish SapariyaPosted Nov 26, 2013, 2:42 AM
select *, (case when Date1 > Date2 then 0 else DATEDIFF(D,Date1,Date2)end) as TotalDays from table
hope this will help you