Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert negative value to zero in mysql

Tags:

People also ask

How do I change negative values to zero in SQL?

select name, (case when (sum(case when TransTypeName like 'credit%' then amount else 0 end) - sum(case when TransTypeName like 'Debit%' then amount ) else 0 end)) *5/100)<0 then 0 else (sum(case when TransTypeName like 'credit%' then amount else 0 end) - sum(case when TransTypeName like 'Debit%' then amount ) else 0 ...

How do I stop negative values in MySQL?

You can create an int field and mark it as UNSIGNED . From MySQL 5.0 Reference Manual: INT[(M)] [UNSIGNED] [ZEROFILL] A normal-size integer. The signed range is -2147483648 to 2147483647.

How do I make a negative number positive in SQL?

We can convert Negative amount to Positive Amount using ABS() function.

Can INT store negative values MySQL?

In case you want to store a negative integer - (INT) will work nicely, unless it's not UNSIGNED. In case you want to store a negative decimal - (DECIMAL) is the way to go, as GBN mentioned above.


When the value of an unsigned field becomes negative it becomes a very big positive number. I use a query like this:

UPDATE table SET foo=foo+bar

I want it to become a zero when it gets negative instead of that.