Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAST unsigned to signed int in MySql

Tags:

mysql

I'm trying to select numbers from database stored as unsigned tinyint But on the output i need them to be negative.

This is what i do:

SELECT
 -CAST(sales_shipments_items.qty AS SIGNED INTEGER) AS qty,
FROM sales_shipments_items
WHERE 1

& this works on my local machine with mysql 5.5.25 But returns 0's on the server with mysql 5.5.32-cll-lve

BTW, on my local machine I do not even have to use CAST it works without it.

I tried to use CONVERT instead of CAST but still only get 0's

Sample data in sales_shipments_items

id|type|shp_num|qty
3   1   3321   2

4   1   3322   2

5   2   3321   2

6   3   3320   1

7   4   3350   1

8   5   3351   3

I'm expecting to see:

-2
-2
-2
-1
-1
-3
like image 856
AnKing Avatar asked Oct 22 '25 05:10

AnKing


1 Answers

This works always. (MYSQL 5.6)

SELECT -1 * CAST(qty as SIGNED) as qty
FROM sales_shipments_items
like image 176
Ibrahim Lawal Avatar answered Oct 23 '25 19:10

Ibrahim Lawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!