Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have question I have Column in My table Revenue , Cost , Order_Type,

I have condition IF Order_Type = 'Success' Then (Revenue - Cost) should display
And if Order_ type Contains 'Return' then (Revenue * -1) and (Cost * -1) should display AS Margin
How i do that can any one help In SQL.

Note - Order_type Have multi-pal Return type like UAE-Return, AE-Return, XYZ-Return like this i just want If Order_Type contains Return value then Above condition should work.

What I have tried:

Declare @OrderType Varchar(Max) = 'Return'



IF CHARINDEX ('Return',@OrderType)> 0
Begin 
Select Revenue * -1 & Cost * -1 ,Sales_Man,Order_Date,Order_Type
From ERP_Source_Table
END
ELSE 
Begin
Select Revenue - Cost 
From ERP_Source_Table
END
 
 Select * from ERP_Source_Table
Posted
Updated 21-Jan-20 21:16pm

Do you mean like this:
SQL
DECLARE @OrderType VARCHAR(MAX) = 'Returns'
IF @OrderType = 'Success' 
   SELECT Revenue - Cost FROM MyTable
ELSE IF @OrderType = 'Return'
   SELECT Revenue * -1, Cost * -1 FROM MyTable
ELSE
   SELECT * MyTable
 
Share this answer
 
Comments
_Asif_ 22-Jan-20 2:40am    
DECLARE @OrderType VARCHAR(MAX) = 'AE-Return'
IF @OrderType = 'Success'
Select Revenue - Cost From ERP_Source_Table
ELSE IF CHARINDEX ('Return',@OrderType) > 0
Select Revenue * -1 , Cost * -1 ,Sales_Man,Order_Date,Order_Type From ERP_Source_Table
ELSE
Select * from ERP_Source_Table

To be precise!
Here is a good tutorial site:
SQL CASE Statement[^]
SQL Server: IF...ELSE Statement[^]
 
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