Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have a SQL table:


SQL
SrNo Name     Amount  ApplyTax
1    John     500      Yes 
2    Peter    700      No
3    Tom      900      Yes


If I want to levy tax at 20% on those who have ApplyTax as 1, how do I do that in one query:

The output I need is
XML
SrNo Name     Amount  Tax
1    John     500      100
2    Peter    700      0
3    Tom      900      180


I am new to SQL so any help would be great.

Thanks,
T
Posted

Try this,
SQL
select SrNo,Name,Amount,case when ApplyTax='Yes' then (Amount*0.2) else 0 end as Tax  from Table

Here I have used (Amount*0.2), which is equal to 20% of Amount.
SQL
20% Amount = (Amount/100)*20
 
Share this answer
 
v3
You can use Case Statement
https://msdn.microsoft.com/en-us/library/ms181765.aspx[^]

SQL
select SrNo,name ,Amount,case when ApplyTax='Yes' then (Amount*20)/100 when ApplyTax='No' then 0 end as Tax From table_name 
 
Share this answer
 
Try:
SQL
SELECT SrNo, Name, Amount, 
   CASE WHEN ApplyTax='Yes' THEN (Amount * 20) / 100 ELSE 0       
   END AS Tax 
FROM MyTable
 
Share this answer
 

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