How do I change a field to a negative based on another field's value in SQL - sql

With the below statement I would like amount to be negative if [Invoice_Type_Code] = 'c', and if is not then it is positive.
SELECT [Invoice_Amount]
FROM [Forefront].[dbo].[VN_GL_DISTRIBUTION_HEADER_MC]
WHERE [Vendor_Code] =' UnitedEL' and
[Date_List1] > '2011-12-31' and
[Date_List1] < '2012-02-01' and
[Company_Code] = 'tmg'

Depending on your RDBMS, using CASE could work -- most RDBMS should support it:
SELECT CASE WHEN Invoice_Type_Code = 'C' THEN -1 ELSE 1 END * Invoice_Amount
FROM [Forefront].[dbo].[VN_GL_DISTRIBUTION_HEADER_MC]
where [Vendor_Code] =' UnitedEL' and
[Date_List1] > '2011-12-31' and [Date_List1] < '2012-02-01' and
[Company_Code] = 'tmg'
This assumes all Invoice_Amounts are positive. If they aren't positive and you still need negative values, use the ABS function and alter your case statement slightly.
Here is the Fiddle.
Good luck.

Related

Case inside 'where' section that changes the condition of an AND statement

I am creating a store procedure and i am wondering how can i add a case block in an Add statement inside the where statement.That case statement checks an input parameter and depending its value it will change the condition from greater that to smaller than and of course be added to the add conditions
So a part of the query is like:
WHERE
AND BM.Example1 IS NOT NULL
AND BM.Example2 IS NOT NULL
AND ( Case When #inputParamter= 'A' THEN AND BM.Example < 0 ELSE And BM.Example> 0 )
ORDER BY 'SEG' ASC, 'CCY' ASC
So by this approach i am thinking to extract an add statement depending on the input parameter but unfortunately i keep getting syntax errors.
Is that possible?
Yepp, just use this:
AND (( #inputParamter= 'A' AND BM.Example < 0) OR ( #inputParamter<>'A' AND BM.Example> 0) )
However, be carefull with NULL, you have to put it in the logic as a third option.
here is a similar answer using case
AND ( Case When #inputParamter = 'A' AND BM.Example < 0 THEN 'Y'
When #inputParamter <> 'A' AND BM.Example > 0 THEN 'Y' ELSE 'N' END = 'Y')

Case When Condition in Where Clause. Use filter condition if it matches the case when condition only

Is it possible to use case when condition in where clause to filter select statement.
For Eg:
Select * from table_name
where source ='UHC'
and
to_char(termdate,'YYYYMM') <= '201603';
But i want second filter condition to work only if policy number is '1'. For Eg:
case when policy_number = '1' then to_char(termdate,'YYYYMM') <= '201603';
if the policy number is not 1 then only 1st where clause should work but if policy number is 1 then both the where clause should work.
i hope i made my situation clear.
You don't need case at all:
Select * from table_name
where source ='UHC'
and ((policy_number = '1' and to_char(termdate,'YYYYMM') <= '201603')
or nvl(policy_number, '0') != '1');
With case condition will be like:
where source ='UHC' and case when policy_number = '1' then to_char(termdate,'YYYYMM') else '000000' end <= '201603');
in else you need something that is always less than '201603'. Another problem here is why you're comparing numbers as varchars? Is it really what you need?

Convert negative data into positive data in SQL Server

The current data of the table is:
a b
---------
-1 5
-11 2
-5 32
My request is to convert every data into a positive value.
Unfortunately, I forgot the name of the built-in function of SQL Server that enables to convert.
You are thinking in the function ABS, that gives you the absolute value of numeric data.
SELECT ABS(a) AS AbsoluteA, ABS(b) AS AbsoluteB
FROM YourTable
The best solution is: from positive to negative or from negative to positive
For negative:
SELECT ABS(a) * -1 AS AbsoluteA, ABS(b) * -1 AS AbsoluteB
FROM YourTable
For positive:
SELECT ABS(a) AS AbsoluteA, ABS(b) AS AbsoluteB
FROM YourTable
UPDATE mytbl
SET a = ABS(a)
where a < 0
Use the absolute value function ABS. The syntax is
ABS ( numeric_expression )
An easy and straightforward solution using the CASE function:
SELECT CASE WHEN ( a > 0 ) THEN (a*-1) ELSE (a*-1) END AS NegativeA,
CASE WHEN ( b > 0 ) THEN (b*-1) ELSE (b*-1) END AS PositiveB
FROM YourTableName
Update all negatives to positive
UPDATE my_table
SET value = ABS(value)
WHERE value < 0
Get all the negative
SELECT value FROM my_table
WHERE value < 0
Get all the negatives and convert them to positive
SELECT ABS(value) FROM my_table
WHERE value < 0

Y or N in an SQL result

Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
For MySQL:
SELECT IF(column > 25, 'Y', 'N')
FROM table
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
There is a IF block for SQL.
http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html
Use the SQL CASE statement
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
select
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
Select If(myColumn > 25, 'Y', 'N') From myTable
Apart from the IF and CASE statements another valid alternative is to create a user defined function feeding in your column value and returning Y or N.
This has the not inconsiderable advantage that if you are selecting on this criteria in more than on SQL statement and at a later date your condition changes - to over 30 say - then you have only one place to change your code. Although using a function adds complexity and overhead so is not always optimal don't underestimate the maintenance advantage of this approach. A minor plus too is you can make the name of the function something more meaningful, and hence self documenting, than an inline CASE

How do I create an If-Then-Else in T-SQL

I have some negative values coming back from a query. I would like them to just be zero.
How do I write a condition in my sql query that returns zero if the value is below a certain value.
sol:
CASE WHEN CONVERT(float,dt.FQI53X02_101) < 1.3 THEN 0 ELSE CONVERT(float,dt.FQI53X02_101) END AS FQI53X02_101
You dont use If-Then-Else in the actual query (you can use them but thats something else)...
What you use is a Case statement... Try this
Select
Case When [Value] < 0 Then 0 Else [Value] End
From
Example
If you want it as part of your query, wrap the return inside a CASE statement. Example from MSDN is below
SELECT 'Price Category' =
CASE
WHEN price IS NULL THEN 'Not yet priced'
WHEN price < 10 THEN 'Very Reasonable Title'
WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
ELSE 'Expensive book!'
END,
CAST(title AS varchar(20)) AS 'Shortened Title'
FROM titles
ORDER BY price
( ABS(Value) + Value ) / 2
edit - this doesn't work now the question has changed