want to convert negative number to positive - sql-server

I have a below query which I have used this in a procedure and want to convert one number into positive as right now it is negative.
UPDATE SHIPMENT
SET TOTAL_SHIP_UNIT_COUNT = (
CASE
WHEN V_SHIP_UNIT_COUNT > v_diff_cost
THEN V_SHIP_UNIT_COUNT - v_diff_cost
ELSE v_diff_cost - V_SHIP_UNIT_COUNT
END)
WHERE SHIPMENT_GID = v_shipment_id;
COMMIT;
in this query v_diff_cost value is negative so while performing ( V_SHIP_UNIT_COUNT - v_diff_cost) action it is adding both the values so if I will convert v_diff_cost value to positive then while subtracting it will give me the right result.
Suppose V_SHIP_UNIT_COUNT value is 33 and v_diff_cost value is -10 then in this case it should perform the action as 33-10 = 23 but it is doing as 33-(-10)= 43 and this should not happen.
kindly help me out.
Thanks

Use ABS() function,
MSDN : A mathematical function that returns the absolute (positive) value of the specified numeric expression.
UPDATE SHIPMENT
SET TOTAL_SHIP_UNIT_COUNT = (
CASE
WHEN V_SHIP_UNIT_COUNT > v_diff_cost
THEN V_SHIP_UNIT_COUNT - ABS(v_diff_cost)
ELSE ABS(v_diff_cost) - V_SHIP_UNIT_COUNT
END)
WHERE SHIPMENT_GID = v_shipment_id;
COMMIT;

Try this code:
UPDATE SHIPMENT
SET TOTAL_SHIP_UNIT_COUNT = (
CASE
WHEN V_SHIP_UNIT_COUNT > v_diff_cost
THEN V_SHIP_UNIT_COUNT - abs(v_diff_cost)
ELSE v_diff_cost - abs(V_SHIP_UNIT_COUNT)
END)
WHERE SHIPMENT_GID = v_shipment_id;
COMMIT;

Try this:
UPDATE SHIPMENT
SET TOTAL_SHIP_UNIT_COUNT = ABS(V_SHIP_UNIT_COUNT - ABS(v_diff_cost))
WHERE SHIPMENT_GID = v_shipment_id;

Related

Store Operators like >= OR <= in Microsoft Access

I have certain values that are needed for validation in Forms, either they look like Value X >= 0 but it could also be X <= 0, it depends on what operator should be used. How can I store such a value?
(I use MS SQL Server + Access as Frontend)
I basicly wanna store the Value and if it needs to be bigger than or smaller than.
Store the value, as usual, in a field, and the operator in another field as Short Text.
The you can use Eval:
Result = Eval("" & [ValueField] & [OperatorField] & "0")
You can store your value as it is, and to check if this value is positive or not you have two ways
First one
Create a computed column to check the Value column as
CREATE TABLE YourTable(
YourValue INT,
IsPositive AS CASE WHEN YourValue < 0 THEN 0 ELSE 1 END
);
INSERT INTO YourTable (YourValue) VALUES
(1), (-1);
SELECT *
FROM YourTable;
Second one
Use a CASE expression (or even you can create a view) as
SELECT CASE WHEN YourValue < 0 THEN 0 ELSE 1 END IsPositive,
--...
FROM YourTable;

Alternative query for calculating percentage

In SQL Server for calculating percentage I have a function like below:
CREATE FUNCTION [dbo].[fuGetPercentage] ( #part FLOAT, #total FLOAT )
RETURNS FLOAT
AS
BEGIN
DECLARE #Result FLOAT = 0, #Cent FLOAT = 100;
IF (isnull(#total, 0) != 0)
SET #Result = isnull(#part, 0) * #Cent / #total;
RETURN #Result
END
I wonder that is there any better alternative for that, with same checks and a better calculating percentage like below:
SELECT (CASE ISNULL(total, 0)
WHEN 0 THEN 0
ELSE ISNULL(part, 0) * 100 / total
END) as percentage
I want to use it directly after SELECT like above.
There is one issue with using functions such as ISNULL. The query will not use indexes in that case. If the beauty of the code isn't in the first place then you can do something like that:
SELECT
CASE WHEN total * part <> 0 /* will check that both total and part are not null and != 0*/
THEN part * 100 / total
ELSE 0
END AS percentage;
I use #DmitrijKultasev answer but now, I found that it has two problems:
Error on conversion because of the overflow of result of multiply.
Performance problem; because of that math multiply and its conversion.
So I change it to this:
SELECT
CASE
WHEN total <> 0 AND part <> 0 THEN -- This will return 0 for Null values
part * 100 / total
ELSE
0
END AS percentage;

Need to verify I am understanding SQL Logic

I would like to know if I have this SQL logic decoded correctly. Here is the the SQL:
,[hrs].[Hours] - SUM(CASE WHEN [UnitState].[UnitStateType] <> 'ACTIVE' THEN [Allocation].[AllocatedEnergyMwh] ELSE 0 END / CAST([Unit].[NetDependableCapacity] AS FLOAT)) AS SH
I interpret this as saying:
if [UnitState].[UnitStateType] does not equal active then SH equals the sum of [Allocation].[AllocatedEnergyMwh] /
(float)[Unit].[NetDependableCapacity].
else SH = [hrs].[Hours]
Not exactly.
There is no else for SH. The else only affects the sum aggregate. More accurately, it says:
SH = hours -
(the sum of AllocatedEnergyMwh where StateType != ACTIVE) / NetDependableCapacity
The else is only used to ignore the active AllocatedEnergyMwh in the sum. It does this by setting AllocatedEnergyMwh = 0 in those cases.
It's close but you missed the [hrs].[Hours] - case part for the first one.
if [UnitState].[UnitStateType] does not equal active
then SH equals [hrs].[Hours] minus the sum of [Allocation].[AllocatedEnergyMwh] / (float)[Unit].[NetDependableCapacity].
else SH = [hrs].[Hours] minus (0)/[Unit].[NetDependableCapacity]
if [UnitState].[UnitStateType] does not equal active then
[hrs].[Hours] minus sum([Allocation].[AllocatedEnergyMwh] / [Unit].[NetDependableCapacity])
otherwise
[hrs].[Hours] minus sum(0/[Unit].[NetDependableCapacity]) -->meaning [hrs].[Hours] - 0

Can TSQL convert from boolean to BIT

I was creating a function that returns BIT, I tried to "Return #count < 1", that did not work, how to convert boolean to BIT in TSQL.
Thanks
You will need to have a conditional statement:
if #count < 1
return 1
else
return 0
Or you could use a CASE statement:
case
when #count < 1 then return 1
else return 0
end
Can count ever be negative? And count should be integer
So what you want is "1 if #COUNT = zero, zero otherwise"
RETURN 1 - SIGN(#COUNT)
Or a simple transmogrification of Shark's answer:
return case
when #Count < 1 then 1
else 0
end
Note that a CASE may have as many WHEN clauses as you need.
Trivia: Curiously, a BIT can be set to 'TRUE' or 'FALSE'. Yeah, quoted strings. Go figure.

AND/OR based on variable value in stored procedures

I would like to use AND/OR between the conditions in a stored procedure, and the decision is dependent on the parameter value whether it was 0 (AND) or 1 (OR)
Can anyone help me with this please, i guess this is an easy thing to do but i can't seem to figure it out. Thanks
The easiest way (on first glance) would be to concatenate the query string using dynamic SQL, but dynamic SQL has its issues.
See The Curse and Blessings of Dynamic SQL for an in-depth explanation.
So I would try to avoid dynamic SQL, which is no big deal if your queries are not too complex.
The easiest way is just to fire two different queries depending on the parameter value:
CREATE PROCEDURE spTest
#AndOr bit
AS
BEGIN
if #AndOr = 0 begin
select * from YourTable where foo = 1 and bar = 2
end
else begin
select * from YourTable where foo = 1 or bar = 2
end
END
This is of course an example with a very simple query.
If you have lots of queries, or if your queries are very complex, this might not be the best solution because it forces you to duplicate all queries...but as always, it depends :-)
You can implement your logic on a CASE statement. Something like this:
CREATE PROCEDURE dbo.MySP #OrAnd BIT
AS
BEGIN
SELECT *
FROM MyTable
WHERE CASE WHEN Condition1 AND Condition2 AND #OrAnd = 0 THEN 1
WHEN (Condition1 OR Condition2) AND #OrAnd = 1 THEN 1 ELSE 0 END = 1
END
If you convert the simple conditions' boolean results into numeric ones (0 or 1), you will be able to use your parameter in the following way:
(
(CASE WHEN condition1 THEN 1 ELSE 0 END ^ #AndOr)
&
(CASE WHEN condition2 THEN 1 ELSE 0 END ^ #AndOr)
) ^ #AndOr = 1
Here #AndOr is your parameter, ^ is the Transact-SQL bitwise exclusive OR operator, & stands for the bitwise AND in Transact-SQL, and the CASE expressions are used to convert the boolean results into 0 or 1.
If #AndOr = 0 (which means we want AND between the conditions), the above expression effectively boils down to this:
case1 & case2 = 1
because X XOR 0 yields X and so neither individual values of case1 and case2 nor the entire result of the & operator are not affected by the ^ operators. So, when #AndOr is 0, the result of the original expression would be equivalent to the result of condition1 AND condition2.
Now, if #AndOr = 1 (i.e. OR), then every ^ operator in the expression returns the inverted value of its left operand, in other words, negates the left operand, since 1 XOR 1 = 0 and 0 XOR 1 = 1. Therefore, the original expression would essentially be equivalent to the following:
¬ (¬ case1 & ¬ case2) = 1
where ¬ means negation. Or, converting it back to the booleans, it would be this:
NOT (NOT condition1 AND NOT condition2)
According to one of De Morgan's laws,
(NOT A) AND (NOT B) = NOT (A OR B)
Applying it to the above condition, we get:
NOT (NOT condition1 AND NOT condition2) = NOT (NOT (condition1 OR condition2)) =
= condition1 OR condition2
So, when #AndOr is 1, the expression given in the beginning of my answer is equivalent to condition1 OR condition2. Thus, it works like expected based on the value of #AndOr.
Having the input parameter you can use a IF clause to make different selects.
If input parameter = 0 make the AND conditions, otherwise make the OR conditions.
I can't see any particular elegant way to do it. So here's the straightforward approach
create function myfun (#parm1 int, #parm2 int, #andor int) returns int
begin
if (#andor = 0 AND #parm1 = 99 AND #parm2 = 99) return 1
else if (#andor = 1 AND (#parm1 = 99 OR #parm2 = 99)) return 1
return 0
end
go
select dbo.myfun(99,98,0) -- AND condition should return 0
select dbo.myfun(99,98,1) -- OR condition should return 1
select dbo.myfun(98,98,0) -- AND condition should return 0
select dbo.myfun(98,98,1) -- OR condition shoujld return 0

Resources