Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL View decimal place

Tags:

I have a MySQL view called Balance created from 2 tables order and income with PHPMyAdmin and contains some calculated fields ex: CustomerBalance the decimal place become automatically 8, I mean the field Type is decimal(50,8)
How can i make it 2 only ?

like image 270
Yahia Baiba Avatar asked Oct 11 '16 13:10

Yahia Baiba


People also ask

How do I get decimal part in MySQL?

FORMAT() function ##' which is rounded upto the number of decimal places specified (in the second argument) and returns the result as a string.

How do I find decimal places in SQL?

For a column named "Column" from a table named "Table", the following query will produce a the count of each row's decimal places: select length( substr( cast(Column as text), instr(cast(Column as text), '.

How do I ROUND to 2 decimal places in MySQL?

ROUND() Function in MySQL. The ROUND() function in MySQL is used to round a number to a specified number of decimal places. If no specified number of decimal places is provided for round off, it rounds off the number to the nearest integer.


2 Answers

You can use truncate

 SELECT TRUNCATE(1.999,2);

return 1.99

 select TRUNCATE(your_column,2) from your_table;
like image 65
ScaisEdge Avatar answered Oct 13 '22 19:10

ScaisEdge


In the select list where you calculate the CustomerBalance expression, explicitly truncate or round (depending on your requirements) the result to 2 digits:

select ... round(..., 2) as CustomerBalance ...
like image 39
Shadow Avatar answered Oct 13 '22 19:10

Shadow