How to write Case statement in SSIS expression - sql

I have a small condition in SQL Code. How can I write that in SSIS in Derived column task.
CASE WHEN SaleProduct IS NULL AND ReProduct IS NULL THEN ''
WHEN SaleProduct IS NOT NULL AND ReProduct IS NULL THEN SaleProduct
WHEN SaleProduct IS NULL AND ReProduct IS NOT NULL THEN SaleProduct
ELSE
SaleProduct +';'+ ReProduct END As COL
I have tried with the following expression
but not getting anything
(ISNULL( [SaleProduct] )&& [ReProduct] == "") : ? "0"
" ------------------------"
UNKNOWN
can anyone help me on this ...

CASE statements can be replicated by using Conditional operator in the Derived column transformation.
For your condition the expression would be like
((ISNULL(SaleProduct) == True && ISNULL(ReProduct) == True) ? " " :((ISNULL(SaleProduct) == False && ISNULL(ReProduct) == True)? SaleProduct :(ISNULL(SaleProduct) == True && ISNULL(ReProduct) == False)? SaleProduct : SaleProduct+";"+ReProduct)

You need to use Ternary operator in Derived column transformation:
To understand and depict your condition:
( ISNULL([SaleProduct]) && ISNULL([ReProduct]) ) ? " " :
( !ISNULL([SaleProduct]) && ISNULL([ReProduct]) ) ? [SaleProduct] :
( ISNULL([SaleProduct]) && !ISNULL([ReProduct]) ) ? [SaleProduct] : ( [SaleProduct]+";"+[ReProduct] )
But tabs causes error in Derived column transformation. Hence it should actually be:
(ISNULL([SaleProduct]) && ISNULL([ReProduct])) ? " " : (!ISNULL([SaleProduct]) && ISNULL([ReProduct])) ? [SaleProduct] : (ISNULL([SaleProduct]) && !ISNULL([ReProduct])) ? [SaleProduct] : ([SaleProduct]+";"+[ReProduct])

Related

Translate a SQL CASE Statement into LINQ [duplicate]

This question already has answers here:
linq case statement
(6 answers)
Closed 3 years ago.
I am looking for assistance translating a SQL CASE Statement into LINQ code. Thing is, I need to implement it into code that already exists.
Basically, I need to transform memberIssues.IsClosed into the Alert Status CASE Statement below, but I've been having some difficulties.
List<IssueRowDTO> issuesList = new List<IssueRowDTO>();
if (userRoleInfo.CanViewAllIssues)
{
#region View All Issues LINQ
// If user can view all issues, then User ID check is removed.
issuesList = (from memberIssues in dbContext.MemberIssues
from issueSteps in dbContext.IssueSteps.Where(issueSteps => issueSteps.ID == memberIssues.CurrentStepID).DefaultIfEmpty()
from issueDefs in dbContext.IssueDefs.Where(issueDefs => issueDefs.ID == memberIssues.IssueDefsID).DefaultIfEmpty()
from member in dbContext.Member.Where(member => member.ID == memberIssues.MemberID).DefaultIfEmpty()
from memberTeam in dbContext.MemberTeam.Where(memberTeam => memberTeam.MemberID == memberIssues.MemberID
&& memberTeam.RoleID == issueSteps.RoleID).DefaultIfEmpty()
from userRoles in dbContext.UserRoles.Where(userRoles => userRoles.RoleID == memberTeam.RoleID
&& userRoles.UserID == memberTeam.UserID).DefaultIfEmpty()
from users in dbContext.Users.Where(users => users.ID == userRoles.UserID).DefaultIfEmpty()
from plans in dbContext.Plans.Where(plans => plans.ID == member.PlanID).DefaultIfEmpty()
where memberIssues.CreatedDate > searchFromDate
select new IssueRowDTO
{
IssueID = memberIssues.ID,
MemberID = memberIssues.MemberID,
ActionRequiredBy = users.FirstName + " " + users.LastName,
MemberName = member.FirstName + " " + member.LastName,
MLTC = plans.PlanName,
Issue = issueDefs.IssueName,
CreatedDate = memberIssues.CreatedDate,
LastUpdate = memberIssues.LastUpdate ?? memberIssues.CreatedDate,
NextDeadline = memberIssues.NextDeadline ??
memberIssues.CreatedDate,
IsClosed = memberIssues.IsClosed,
ClosedOffService = memberIssues.ClosedOffService ?? "",
SubIssueDefsID = memberIssues.SubIssueDefsID ?? 0,
CurrentStepNum = issueSteps.StepNum
})
.ToList();
SQL
CASE
WHEN CurrentStepNum > 1 THEN 'Positive'
WHEN CurrentStepNum = 1 AND memberIssues.IsClosed <> 1 THEN 'Pending'
WHEN CurrentStepNum = 1 THEN 'False Positive'
END AS 'Alert Status'
I cheated (a little). Assuming StepNum is always >= 1, you could remove the == 1 test altogether as well.
AlertStatus = (issueSteps.StepNum > 1 ? "Positive" :
issueSteps.StepNum == 1 ? (!memberIssues.IsClosed ? "Pending" : "False Positive"))
A more literal translation would be:
AlertStatus = (issueSteps.StepNum > 1 ? "Positive" :
issueSteps.StepNum == 1 && !memberIssues.IsClosed ? "Pending" :
issueSteps.StepNum == 1 ? "False Positive" : "Bad StepNum!")

In Angular ngClass can we combine more than one conditions?

[ngClass]="{'hasDocument': item.communication_file != '', 'no_Doc': item.communication_file == '' }">
For example, how to do the following?
[ngClass]="{'hasDocument': item.communication_file != '' && item.communication_file != null, 'no_Doc': item.communication_file == '' }">
Is it possible to do so? If yes, what is the correct syntax?
But you are missing "()" in this braces define "True" and "False".
[ngClass]="{'hasDocument': (item.communication_file != '' && item.communication_file != null), 'no_Doc': item.communication_file == '' }"
if you want to use more than one condition use braces.

Filter Data based on priority in LINQ

The fields in my Rate Table are Route, VehicleMasterId, VehicleType, Material, Client, UnitRate etc.
The priority order on which I have to fetch a single row is : VehicleNo > Route > Client > VehicleType, Material
Suppose I have 2 rows with same data except 1 has Client and Vehicle Type and the other one has VehicleNo.Then based on my priority, I should pick the rate of the row with VehicleNo.
To excute this In linq I have first picked all the rows with matching data. Here is my code.
public RateMasterDataModel GetRateMasterforCn(Consignment cn){
// I will always pass all the above fields in cn
var rateMaster = (from rate in Context.RateMaster
where rate.FromDate <= cn.Cndate
&& rate.ToDate >= cn.Cndate
&& (rate.VehicleTypeId != null ? rate.VehicleTypeId == cn.VehicleTypeId : true)
&& (rate.VehicleMasterId != null ? rate.VehicleMasterId == cn.VehicleMasterId : true)
&& (rate.ClientId != null ? rate.ClientId == cn.ClientId : true)
&& (rate.RouteId != null ? rate.RouteId == cn.RouteId : true)
&& (rate.MaterialMasterId != null ? rate.MaterialMasterId == cn.MaterialMasterId : true)
select new RateMasterDataModel
{
RateMasterId = rate.RateMasterId,
FromDate = rate.FromDate,
ToDate = rate.ToDate,
ClientId = rate.ClientId ,
VehicleMasterId = rate.VehicleMasterId,
VehicleTypeId = rate.VehicleTypeId,
MaterialMasterId = rate.MaterialMasterId,
UnitRate = rate.UnitRate,
LoadTypeId = rate.LoadTypeId,
LoadingPointId = rate.RouteId,
CalculationMasterId = rate.CalculationMasterId
}).ToList();
}
Please suggest how to filter after this.
You can use below code to get records ordered by VehicleNo > Route
.OrderBy(v=>v.VehicleNo).ThenBy(r=>r.RouteId)
Add multiple .ThenBy() clause as per your column requirement for sorting the data.
You mean to say if the row which doesn't have the vehicalno. filld-up then the row having Route must be selected.is it correct?

How to make an IF Statement in SSIS

How can I create a condition in SSIS like below?
I know how to create it just in SQL Server
Example:
IIF(LEFT(MAT,1) = '"', NULL, REPLACE(MAT,'""',''))
«boolean_expression» ? «when_true» : «when_false»
LEN([Mobile]) == 9 ? ("0" + [Mobile]) : [Mobile]
SET MAT = LEFT(MAT,1) == '"' ? NULL : REPLACE(MAT,'""','');
IFF statement works condition-based assignment
The IIF() statement has the following format:
=IIF( Expression to evaluate,
what-to-do when the expression is true,
what-to-do when the expression is false )
Parameter1: It should be a Boolean Expression.
Paremeter2: This value will return when Expression is true.
Paremeter3: This value will return when Expression is false.
The syntax in SSIS is like this: [condition] : [value if true] ? [value if false] With that said, you could do...
(MAT,1)='' : NULL ? REPLACE(MAT,'""',''))

SQL statement filter result

I need to filter the SQL query result according to 3 conditions:
1. Location
2. Doctor Name
3. Specialty Name
Below is the sql query if all 3 conditions are not empty:
if (location != "" && doctor!="" && specialty!="")
select Location, ...
from clinicdoctors
where Location = #Location and DoctorName = #DoctorName and SpecialtyName = #SpecialtyName
}
if only location is empty,
if (location == "" && doctor!="" && specialty!="")
select Location, ...
from clinicdoctors
where Location is not null and DoctorName = #DoctorName and SpecialtyName = #SpecialtyName
...
If I wanna check all the conditions, I need to write eight if statements.
What should I do to simplify the code in this situation?
select Location, ...
from clinicdoctors
where
ISNULL(#Location,Location) = Location
and ISNULL(#DoctorName,DoctorName) = DoctorName
and ISNULL(#SpecialtyName,SpecialtyName) = SpecialtyName
I think it would be better to do this in the code than sql as you have done. Not really any way around it since the queries are so different, but a terse way to do it would be:
$loc_where = empty($loc) ? 'Location IS NOT NULL' : "Location = $loc";
$doc_where = empty($doc) ? 'AND DoctorName IS NOT NULL' : "AND DoctorName = $doc";
$spec_where = empty($spec) ? 'AND SpecialtyName IS NOT NULL' : "AND SpecialtyName = $spec";
query ... WHERE $loc_where $doc_where $spec_where