Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
declare @t table
(
Maxmonth int,
startmonth varchar,
Duration int,
endmonth  varchar,
fixedAmount money,
RefferalAmount int
)
insert @t (Maxmonth,startmonth,Duration,endmonth,fixedAmount,RefferalAmount)
select DATEPART(MM,start_date) + fixeddDuration-1,
DATEPART(MM,start_date),
fixeddDuration ,
DATEPART(MM,end_date),
fixedAmount,
case when ( start_date >= Datepart(MM,start_date)) AND ( end_date <= Datepart(MM,end_date))
 then fixedAmount/nullif(fixeddDuration,0)
 ELSE 0
 END
from TC_Project_Referal
where id_tc_projuser = 14
select * from @t


actually i want to calculate the refferalamount . here the fixedamount is given in database from the frontend..lets suppose 1000$ and duration is 6 months

so we need to calculate based on the date 2-1-2012 startdate and 6-1-2012 is enddate here the period of time is 6 months form feb1st to july 1st so we need to calculate.

1>=2 & 1<=7 ---1st month -- here we should not allow because the start date is from feb but it is jan so it is not allowed to calculation

2>=2 & 2<=7 --2nd month

3>=2 & 3<=7 --3rd month

4>=2 & 4<=7 --4th month

5>=2 & 5<=7 --5th month

6>=2 & 6<=7 --6th month

7>=2 & 7<=7 --7th month

8>=2 & 8<=7 --7th month---here calculation is not allowed because it is the 8th month but when you do for 8th month the referralamount will be show as zero because the duration period is only for 6 months as we take like that...it is based on the user


am getting error in case .....when And is used it is not working showing the result as 0 but when i use OR it is showing the result.....
Posted
Updated 3-Mar-14 20:50pm
v2

1 solution

I cannot understand what are you trying to achieve here but you seem to be comparing date and month in the case statement.
 
Share this answer
 
Comments
Sharath Kumar 4-Mar-14 7:01am    
case when ( start_date >= Datepart(MM,start_date)) AND ( end_date <= Datepart(MM,end_date))
then fixedAmount/nullif(fixeddDuration,0)
ELSE 0
END
actually i want to calculate the refferalamount . here the fixedamount is given in database from the frontend..lets suppose 1000$ and duration is 6 months

so we need to calculate based on the date 2-1-2012 startdate and 6-1-2012 is enddate here the period of time is 6 months form feb1st to july 1st so we need to calculate.

1>=2 & 1<=7 ---1st month -- here we should not allow because the start date is from feb but it is jan so it is not allowed to calculation

2>=2 & 2<=7 --2nd month

3>=2 & 3<=7 --3rd month

4>=2 & 4<=7 --4th month

5>=2 & 5<=7 --5th month

6>=2 & 6<=7 --6th month

7>=2 & 7<=7 --7th month

8>=2 & 8<=7 --7th month---here calculation is not allowed because it is the 8th month but when you do for 8th month the referralamount will be show as zero because the duration period is only for 6 months as we take like that...it is based on the user


am getting error in case .....when And is used it is not working showing the result as 0 but when i use OR it is showing the result.....
dan!sh 4-Mar-14 8:40am    
Really! You just copy/pasted the question!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900