Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value is positive or negative

Tags:

sql

tsql

I am trying to write a query which checks if 2 amounts have the same sign (+ or -). Do you have a good solutions that can be used?

Something like: If SAMESIGN(value1, value2) then do something

like image 435
MrProgram Avatar asked Aug 04 '14 09:08

MrProgram


2 Answers

CASE WHEN (value1 * value2) < 0 THEN 'DiffSign' ELSE 'SameSignOrOneIsZero' END
like image 131
Giannis Paraskevopoulos Avatar answered Oct 31 '22 22:10

Giannis Paraskevopoulos


Try to use sign function as below

case 
when sign(value1)=sign(value2) then 'the same sign'
else 'different sign'
end
  • SIGN function
like image 28
Robert Avatar answered Oct 31 '22 20:10

Robert