Home All Groups Group Topic Archive Search About

Using CASE to return positive or negative numbers

Author
12 May 2005 7:26 PM
Terri
I am trying to use the case expression to change a number from positive to
negative or vice versa.

If QTY = 50 I want to return 50 when TRANS_TYPE = 'A' and -50 when
TRANS_TYPE = 'B'

If QTY = -10 I want to return -10 when TRANS_TYPE = 'A' and 10 when
TRANS_TYPE = 'B'

Psuedo code here:

SELECT
(CASE   TRANS_TYPE
WHEN 'A' THEN QTY
WHEN 'B' THEN ??? (here is where I want to return negative if the number is
positive or positive if the number is negative)
END)as QTY,
TRANS_TYPE,
FROM TABLE1

Author
12 May 2005 9:26 PM
--CELKO--
SQL has a unary minus operator since the SQL-86 standards.

CASE trans_type WHEN 'A' THEN qty ELSE -qty END
Author
13 May 2005 2:54 PM
Terri
Thanks to all.

Is it appropriate to respond back to the newsgroup to say your issue is
resolved or is that considered unnecessary noise.

Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1115933208.090797.57720@g44g2000cwa.googlegroups.com...
> SQL has a unary minus operator since the SQL-86 standards.
>
> CASE trans_type WHEN 'A' THEN qty ELSE -qty END
>
Author
13 May 2005 3:32 PM
Louis Davidson
It is a nice touch.  Most of us wonder if the advice given is actually of
any value :)

--
----------------------------------------------------------------------------
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP


Show quote
"Terri" <te***@cybernets.com> wrote in message
news:d62fu6$ug7$1@reader2.nmix.net...
> Thanks to all.
>
> Is it appropriate to respond back to the newsgroup to say your issue is
> resolved or is that considered unnecessary noise.
>
> "--CELKO--" <jcelko***@earthlink.net> wrote in message
> news:1115933208.090797.57720@g44g2000cwa.googlegroups.com...
>> SQL has a unary minus operator since the SQL-86 standards.
>>
>> CASE trans_type WHEN 'A' THEN qty ELSE -qty END
>>
>
>

AddThis Social Bookmark Button