When and where to use ternary operator? [closed] - operators

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I often read ternary operator(? : ) in books,but I am not clear the best practice of it.
What is the advantages and disadvantages or best practice of it?

The ternary operator ? : represents an if-then-else construct, except that it actually returns a value. So instead of:
if ( condition )
x = expression1;
else
x = expression2;
You can write:
x = (condition) ? (expression1) : (expression2);
In C, C++, Java, etc., you cannot write:
x = if (condition) (expression1) else (expression2);
Some languages, like Ruby, do allow a form like that, however, since statements in Ruby always return a value, and the value of a block is the value returned by the last statement in a block.

You may use the operator as
int nNum1 = 10, nNum2 = 20, nMax = ( nNum1 > nNum2 ) ? nNum1 : nNum2
It is shorthand for simple if-else comparisons, where a variable is assigned based on the result of the comparison.

Related

Incorrect syntax when setting variable equal to a number in WHEN condition of T-SQL CASE statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Context: I have a scalar function that returns a formatted string. The type of formatting performed depends on the length LEN() of the string passed - this is handled with a T-SQL CASE statement.
Question: why am I getting a syntax error when setting variable, #LengthOfString, equal to a number?
Error:
Incorrect syntax near '='
Relevant code:
CREATE OR ALTER FUNCTION FormatString
(#PassedString varchar(255) NULL)
...
DECLARE #FormattedString varchar(255)
DECLARE #LengthOfString int = LEN(#PassedString)
SET #FormattedString = (SELECT
CASE #LengthOfString
WHEN #LengthOfString = 5
THEN RETURN Format(<formatting>)
WHEN ...
Remove #LengthOfString next to the CASE
That statement should look like this:
SET #FormattedString = (SELECT CASE
WHEN #LengthOfString = 5 THEN RETURN Format(<formatting>)
WHEN ...

Why 'NOT LIKE' does not work with OR in SQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why below query does not return CategoryName and Description excluding the record whose Description starts with S and D. But when I replace OR with AND it works.
SELECT CategoryName, Description
FROM [Categories]
Where Description NOT LIKE 'S%'
OR Description NOT LIKE 'D%'
You really want AND instead of OR. But in SQL Server this is more simply written as:
Where Description NOT LIKE '[DS]%'
The reason it doesn't work is you are dealing with boolean logic.
Your query is asking if it is 'not like apples or not like oranges'
OR means both boolean statements need to be false in order to be false (or either needs to be true to be true) depending on your point of view.
if you have an apple it is not like and orange so your boolean logic is 1 or 0 which results in 1 or true.
That is why you want and so you need bothe to be 1 to pass the boolean logic.
Taking SQL out of this, this is a classic application of DeMorgan's Law.
In this case, it looks like you want "Descriptions that start with neither S nor D". Translating that into Boolean logic, that's:
(Description NOT «start with S») AND (Description NOT «start with D»)
Which is equivalent to
NOT ((Description «start with S») OR (Description «start with D»))
In this case, each of the two statements that I'm saying are equivalent is easily translated to SQL - use whichever of them makes the most sense.
It would be nice if you could just stick some wildcards in the in clause like this:
where column1 not in ('%value1%','%value2%','%value3%')
But, you can’t stick a wildcard inside of the IN clause. So, here is the easiest solution.
select *
from table1
where column1 not like '%value1%'
and column1 not like '%value2%'
and column1 not like'%value3%';
Basically you are trying to replace in ( value1 AND value2 AND value3 )
with OR logical operator,
That is why this will not work.
after you replace OR with AND you should get this results:
Link to full description of the problem and the solution

divide by zero in specific case in sql server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am getting Divide by zero error for the following statement.
select ((((57151130.0000000000)+(57612530.0000000000))/2)/((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)))*366
The fact that you are supplying literal values is a little odd, however, one method of avoiding a divide by 0 error is using NULLIF (Transact-SQL).
NULLIF takes 2 parameters. If the values of the 2 expressions for the parameters are the same value, then NULLIF returns NULL, otherwise it returns the value of the first expression. For example (in literal terms):
SELECT NULLIF(1,0), NULLIF('a' + 'b' + 'c','abc');
This returns the values 1 and NULL. For your query, the format would therefore be:
SELECT (((57151130.0000000000+57612530.0000000000)/2)/NULLIF(12548020.0000000000-34321580.0000000000+21773560.0000000000,0))*366
Note I have removed several of the parenthesis as there's no need to wrap every value/expression in a pair. Doing so will likely lower readability.
Then, if the expression under the divisor has the value 0, the value NULL will be returned instead. Considering that NULL represents an unknown value, and {expr}/0 is certainly an unknown value as well, the value would be the most appropriate.
If you then need to represent the NULL value in a particular way, you can wrap the whole expression in a further ISNULL.
In addition, you can use CASE to check zero values statement:
select (
(((57151130.0000000000)+(57612530.0000000000))/2)
/
CASE WHEN((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000)) = 0 THEN NULL
ELSE ((12548020.0000000000)-(34321580.0000000000)+(21773560.0000000000))
END
)*366

ERROR: operator does not exist: integer == integer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am using these statements in a postgres function.
Select count(*) into V_check
from employee
where employee_name like 'Raj%';
if V_check == 0
then
update exception set exception_found = 'Raj';
end if;
I get this error :
ERROR: operator does not exist: integer == integer
LINE 1: SELECT V_check == 0
You should use = instead of ==.
Here is a list of the different comparison operators that you can use:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
As pointed out, the comparison operator for equality is = not ==. However, you should write the condition as:
if not exists (select 1 from employee where employee_name like 'Raj%')
then
update exception
set exception_found = 'Raj';
end if;
This saves you a declaration. Also, not exists is faster than count(*) -- because not exists can stop at the first matching row.
Or dispense with the conditional entirely:
update exception
set exception_found = 'Raj'
where not exists (select 1 from employee where employee_name like 'Raj%');

The isnull function requires 2 argument(s) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
SELECT IIF(ISNULL(MAX((AttTmpInOutFrmMachine.AttId))),0,MAX((AttTmpInOutFrmMachine.AttId)))+1
FROM AttTmpInOutFrmMachine
The second argument in ISNull is value that will be used when your field data is actually null. Example:
IsNUll(someint, 0)
the above will return someint if someint not null, otherwise, it will return 0
This is enough
SELECT ISNULL(MAX(AttTmpInOutFrmMachine.AttId),0)+1 FROM AttTmpInOutFrmMachine
As Syntax for IIF is :
IIF ( boolean_expression, true_value, false_value )
I think you are looking out for logic like below:
SELECT IIF(ISNULL(MAX(AttTmpInOutFrmMachine.AttId),0)=0
,0
,MAX(AttTmpInOutFrmMachine.AttId))+1 as NextAttId
FROM AttTmpInOutFrmMachine
The first is the value that should checked against null and the second one is the value with that null should be replaced.
See here for a exmplanation for ms-sql
Syntax
ISNULL ( check_expression , replacement_value )
Arguments
check_expression Is the expression to be checked for NULL.
check_expression can be of any type. replacement_value Is the
expression to be returned if check_expression is NULL.
replacement_value must be of a type that is implicitly convertible to
the type of check_expresssion.