How to output calculation to 4 decimal places - mysql

I am in SQL Workbench and want my output to include 4 decimal places. I have tried different combinations of casting 'sessions', 'transactions', and 'cvr' as DECIMAL as well as using ROUND, and I can't seem to get the output I'm looking for. 'sessions' and 'transactions' are both in NUMERIC(19,2) format.
Ideally, I want to stay away from casting to float to avoid losing precision.
select cast((transactions/sessions) as decimal(10,5)) as cvr
from(select name
,sum(cast(sessions as decimal(10,5))) as sessions
,sum(cast(transactions as decimal(10,5))) as transactions

How to get output with 4 decimal places
SELECT FORMAT(123.456789, 4) as col
123.4567
SELECT FORMAT(123.456, 4) as col
123.4560

Related

Get values from database with only 2 decimal places using MySQL

I want to retrieve values from a table with figures formatted in float. The decimal places range up to thirteen. I only want to retrieve the rows whose values only have exactly 2 decimal places. How do I do this?
Expected output:
[45.678, 56.236656457, 89.23, 100.89] ==> [89.23, 100.89]
You can use select Length(123.12 % 1) - 2 to get the length of the values after the decimal.
select (123.12 % 1) returns 0.12 so you always know that you have 0. in your result set, you can then get the length of your value minus the two characters and only select the rows where length = 2.

How can I round up the numbers of mysql table, leaving the two decimals to zero. it's possible?

I need round up the numbers of a complete table of mysql with a funtion leaving the two decimals to zero?
e.g.:
from 75,55 to 86,00
from 75,45 to 75,00
All the prices of a complete table changed a few decimal and I do not know how it happened.
Can anybody help me?
You can simply use Round(X, D) function. From Docs:
Rounds the argument X to D decimal places. The rounding algorithm
depends on the data type of X. D defaults to 0 if not specified. D can
be negative to cause D digits left of the decimal point of the value X
to become zero.
Try:
SELECT ROUND(75.45, 2);
-- Fetches 75.00
SELECT ROUND(75.55, 2);
-- Fetches 76.00
Now, it seems that you want to Update all the values of a particular column, by rounding them to two decimal places. You can do the following:
UPDATE your_table_name
SET your_column_name = ROUND(your_column_name, 2);
-- It will round all the values to 2 decimal places.
"UPDATE your_table_name SET your_column_name = ROUND(your_column_name, 2);" not work for me. I dont know.
¡SOLUTION! = TABLE > ESTRUCTURE > NAME COLUMN > length/values > remove decimal.
All number round up in my row (column). Them I put decimal again. All my fields it's ok.

Check decimal places that are not 0

Currently I have this field in my database that is a (16, 10) double. I need to change this to a ( 16, 4 ) double. the only problem is that I have a lot of records and I need to check if some any of those records are using more than 4 decimal places. Something like 1500.3333333. The problem is that by changing just changing the field to (16,4) I get zeros to the right, like 14,5500.
Is there any way, using Mysql that I can check if the double is using more than 4 decimal places that are not 0?
Use CHAR_LENGTH() and LIKE functions, like this:
SELECT * FROM table
WHERE CHAR_LENGTH(column) > 4
AND column NOT LIKE "%0"

How to create query with simple formula?

Hey is there any way to create query with simple formula ?
I have a table data with two columns value_one and value_two both are decimal values. I want to select this rows where difference between value_one and value_two is grater then 5. How can i do this?
Can i do something like this ?
SELECT * FROM data WHERE (MAX(value_one, value_two) - MIN(value_one, value_two)) > 5
Example values
value_one, value_two
1,6
9,3
2,3
3,2
so analogical difs are: 5, 6, 1, 1 so the selected row would be only first and second.
Consider an example where smaller number is subtracted with a bigger number:
2 - 5 = -3
So, the result is a difference of two numbers with a negation sign.
Now, consider the reverse scenario, when bigger number is subtracted with the smaller number:
5 - 2 = 3
Pretty simple right.
Basically, the difference of two number remains same, if you just ignore the sign. This is in other words called absolute value of a number.
Now, the question arises how to find the absolute value in MySQL?
Answer to this is the built-in method of MySQL i.e. abs() function which returns an absolute value of a number.
ABS(X):
Returns the absolute value of X.
mysql> SELECT ABS(2);
-> 2
mysql> SELECT ABS(-32);
-> 32
Therefore, without worrying about finding min and max number, we can directly focus on the difference of two numbers and then, retrieving the absolute value of the result. Finally, check if it is greater than 5.
So, the final query becomes:
SELECT *
FROM data
WHERE abs(value_one - value_two) > 5;
You can also do complex operations once the absolute value is calculated like adding or dividing with the third value. Check the code below:
SELECT *
FROM
data
WHERE
(abs(value_one - value_two) / value_three) + value_four > 5;
You can also add multiple conditions using logical operators like AND, OR, NOT to do so. Click here for logical operators.
SELECT *
FROM
data
WHERE
((abs(value_one - value_two) / value_three) + value_four > 5)
AND (value_five != 0);
Here is the link with various functions available in MySQL:
https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
No, you would just use a simple where clause:
select *
from data
where abs(value_one - value_two) > 5;

MySQL: compare a mixed field containing letters and numbers

I have a field in the mysql database that contains data like the following:
Q16
Q32
L16
Q4
L32
L64
Q64
Q8
L1
L4
Q1
And so forth. What I'm trying to do is pull out, let's say, all the values that start with Q which is easy:
field_name LIKE 'Q%'
But then I want to filter let's say all the values that have a number higher than 32. As a result I'm supposed to get only 'Q64', however, I also get Q4, Q8 and so for as I'm comparing them as strings so only 3 and the respective digit are compared and the numbers are in general taken as single digits, not as integers.
As this makes perfect sense, I'm struggling to find a solution on how to perform this operation without pulling all the data out of the database, stripping out the Qs and parsing it all to integers.
I did play around with the CAST operator, however, it only works if the value is stored as string AND it contains only digits. The parsing fails if there's another character in there..
Extract the number from the string and cast it to a number with *1 or cast
select * from your_table
where substring(field_name, 1, 1) = 'Q'
and substring(field_name, 2) * 1 > 32