Changing decimal places of a Double value MySQL - mysql

I have a database table called druginfo. It contains prices in WSprice column. Type of WSprice column is Double. This means It can contain like 23.5698 values. But I want to show all the values in the column WSprice 2 decimal places rounded like 23.57. How to apply that to all values in the column? Help me to do this.

If you only want to display your DOUBLE column to 2 decimal places, you can use the ROUND function:
SELECT ROUND(column_name, 2)
FROM your_table
This will display a value of 23.5698 as 23.57 in the result set.
If you want to change the format of the entire column you can use this:
ALTER TABLE your_table MODIFY column_name DECIMAL(9, 2)

I think we need to use CAST() instead of ROUND().
The reason behind is ROUND() return decimal values when decimal value exists in the database.
Example:
SELECT ROUND(columnName, 2)
FROM tableName
if columnName = 10.5 Output will be like 10.50
But if columnName = 10 Output will be like 10
And CAST() will return decimal value. But we need DECIMAL with CAST
SELECT CAST(columnName AS DECIMAL(10,2))
FROM tableName
The output will be like 10.00

Related

Remove last digit from all data in column in MySQL

Just asking if this is possible:
I have a mysql database 'data' and a column 'count' which contains numbers such as
3741
49215
345
4686794
I was wondering if there is a possibility to remove the last figure from each number in this column, so there will be these values:
374
4921
34
468679
(for 100,000 rows so i can't do it manually :) )
Thank you!
This will work if the field is numeric or string and is always > 0:
UPDATE `table` SET `count` = FLOOR(`count`/10)
Note single digit values will become 0
If some numbers are negative, it will not give the right answer and a substring approach like the other answers is better.
Use the MySQL SUBSTRING function to extract portion of a string. Use CHAR_LENGTH function to calculate the number of characters in the string.
SELECT
col,
SUBSTRING(col, 1, CHAR_LENGTH(col) - 1) AS col_trimme
FROM tbl
is the data type is string you can
update my_table
set my_column = substr(my_column, 1, length(my_column) -2)
if is an int you can cast
update my_table
set my_column = cast(substr(cats(my_column AS varchar(16)), 1, length(my_column AS varchar(16)) -1), AS INT)
This will work if the field is numeric or string:
UPDATE table SET count = SUBSTRING(count, 1, CHAR_LENGTH(count) - 1);

How to get a numeric signature of a CHAR column in MySql

Given a CHAR or VARCHAR column in MySql, what I need is to convert it to a number so it can be summed up. For example, if the column name is CHAR1, the SELECT would be:
SELECT SUM(ConvertToNumber(CHAR1)) from TABLE1
The function ConvertToNumber just needs to convert to a number, it doesn't matter which number as long as it always converts to the same.
Is this feasible with native or user defined functions?
UPDATE: To be clear, the values of CHAR1 can be any string of alphanumeric characters.
What you can do is convert the column to the hexadecimal format, and then convert this result into base 10 to get an integer.
SELECT SUM(CONV(HEX(CHAR1), 16, 10)) FROM TABLE1;
For instance:
INSERT INTO TABLE1 (CHAR1) VALUES ("foo"), ("bar"), ("baz");
/* "int" value for each entry */
SELECT CONV(HEX(CHAR1), 16, 10) FROM TABLE1;
6713199
6447474
6447482
/* sum of the int values */
SELECT SUM(CONV(HEX(CHAR1), 16, 10)) FROM TABLE1
19608155
SELECT CAST(CHAR1 AS UNSIGNED) FROM TABLE1

How to implement equal/approximate condition for double datatype in mysql?

I have a table with one column of double datatype. I want to select rows by comparing against double value. If there is not exact value exist in the double column, I want to fetch rows containing double value which are equal or closest/approximate.
select *
from tablename
where DoubleColumnName equal/approx "some double value"
You can achieve that with
SELECT * FROM tablename WHERE ABS(DoubleColumnName-$value)<1E-13
here 1E-13 means precision delta (you can adjust it, of cause) and $value is searched value
What is your approximation limits. Assuming you are happy with an error range of +/- 0.5, you can try something like this:
select * from table where (double_value_from_database - SOME_DOUBLE_VALUE) < 0.5
Use the BETWEEN keyword
SELECT * FROM tablename
WHERE
DoubleColumnName BETWEEN "lowerBound" AND "UpperBound"

Sorting an Alphanumeric value mysql

I want to find the max value in a column.
Column values are,
E00004,
A00005,
B00011,
H-00001,
E2100112,
EFQ20098,
ESSF20003
I just want to sort the values by their number, Dont mind about the alphabets. It have to be like this, I'm using MYSQL
E2100112,
ESSF20003,
EFQ20098,
B00011,
A00005,
E00004,
H-00001
Assuming the last 5 digits are the number:
select columnName from tableName
order by convert(int, right(columnName, 5)) desc
As #IkeWalker stated, the number can have an arbitrary size.
For it, you'll have to use a while cycle to check the number.
Or, you can have a function do that for you!
Check this article!

Constraining returned value decimal places in mySQL

I am looking for some help.
I am running the following in mySQL
"SELECT AVG(`readingValue`) AS `readingValue` FROM table
This returns the value - 0.0282982
If possible I would like to return only 4 digits rounded up/down to closest.
In this case 0.0283.
Is this possible in the select string, or will I need to do it in php?
p.s readingValue column is float(4,3)
Would the ROUND function be an option?
SELECT ROUND( AVG(`readingValue`), 4 ) AS `readingValue` FROM table