Link to home
Start Free TrialLog in
Avatar of isames
isames

asked on

Javascript Case Expression

How do you write the equivalent of a SQL Case expression in JavaScript?

Below is what I have so far:
necResult=(QTYCostWithSalesRev_180DayCOGS*2)/(SERVER_FLD0000003);

The above code works, but I need to add to the code that says, 'when QTYCostWithSalesRev_180DayCOGS is 'blank' or zero, then zero else execute the code above.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You can use a Ternary expression - like so

necResult= QTYCostWithSalesRev_180DayCOGS ? (QTYCostWithSalesRev_180DayCOGS*2)/(SERVER_FLD0000003) : 0

Open in new window


Basically the same as

if (QTYCostWithSalesRev_180DayCOGS) {
  necResult =  (QTYCostWithSalesRev_180DayCOGS*2)/(SERVER_FLD0000003) ;
}
else {
  necResult = 0;
}

Open in new window

There is no closed replacement, cause CASE in SQL (at least T-SQL) has two different forms..

One is the switch statement. The other form is a cascading if .. else if.

Please define 'blank' in your context.
Avatar of isames
isames

ASKER

@Ste5an
I mean 0 not blank.

I would you write the SWITCH code?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@isames,

Do you still require assistance with this question? If so post back here, otherwise please can you close the question.

JulianH