TINYTEXT Calculation in MYSQL - mysql

MYSQL Database:
I have a field in a Table Column1 contain the Price value, but declared as TINYTEXT.
I need to multiply the price by 100, but i i cast that value, no value is coming.
I tried both the below option.
SELECT CAST(Column1 as UNSIGNED) * 100
SELECT CAST(Column1 as SIGNED) * 100
TINYTEXT Sample Data
$19.99
$11.99
its a Dolloar Sign ($19.99 and sometimes value is not present in that column.
Regards
Vikram.

Well, you will have to process the string in there to sanitize the data. Based on the fact that $19.99 is the only variation then it seems safe just to replace the dollar sign:
SELECT REPLACE(price, '$', '') * 100 FROM t
However, you should seriously consider turning that column into DECIMAL. That way you won't face a bug when someone adds an euro or pound sign.

Related

Why is MySQL rounding division of 2 decimal to an unexcepted value?

I have a table with the following data:
Area VARCHAR(50),
Revenue DECIMAL(20,2),
Expense DECIMAL(20,2),
PercentBilled DECIMAL(6,2)
These values were imported from a spreadsheet and the percent billed is not precise enough. It's rounded to 2 decimal places. I can calculate it by taking Revenue / Expense, but I'm not getting the value I'm expecting.
Select * from would return 'Counter', -1822.90, 2749.63, 0.66
-1822.90 / 2749.63 = -0.6629619.... which the absolute value would round to 66.3%, which is the precision I need.
So why then, when I run the following query:
Select Area, (Revenue / Expense) AS calcPercentBilled, PercentBilled from
I get: 'Counter, -1822.90, 2749.63, 1.000000, 0.66 ?
My guess is it's something funky with MySQL and types, but I can't figure out what's happening. Why is MySQL rounding the division of 2 decimal numbers in a query to 1.000000?
The problem is the data type you have specified for PercentBilled.
DECIMAL(6,2) means store the value with 6 digits with 2 of them after the decimal point. So to increase the precision of the value stored you'll need to change the type of the column, perhaps to DECIMAL(9,6). This would allow the value in your example to be stored as "0.662962" (i.e. six decimal places).
Note that simply updating the type of the column will not magic up the missing precision unless you re-import your data from source. Simply changing the data type without re-loading the data will change it to "0.660000".

MYSQL Truncated incorrect DOUBLE value: Upon multiplying

I have a monthly income column. I need to get the Annual Income, so I tried multiplying monthly-income to 12.
I already tried casting it to UNSIGNED or CHAR. BUT I STILL GET THE SAME ALERT/ERROR
THE Query goes like this:
for example
monthly=20,000
SELECT monthly*12 from ...
The output should be 240,000. I can't get that output thus getting that mysql alert/error:
Truncated incorrect DOUBLE value: '20,000'.
Remove comma before doing arithmetic operation, if monthly is text datatype
SELECT cast(replace(monthly, ',', '') as decimal(5,2)) * 12 from table

SQL Query - How do I convert a text field containing dollar sign to decimal only (strip out dollar sign)

I am running a query on a MySQl database and I have a field of text characters that contains a dollar amount with a dollar sign and a number (ie $1,345.89) I need to do a check for not NULL (some of the text fields are NULL) and that the value is greater than 0. How can I convert this text field to a decimal value so that I can verify that it is greater than 0. I do not want to alter the original text in the database.
Thanks
Here is a query I tried but it does not work:
SELECT parcel, year, due
FROMdeliquent_property
WHERE year IN ('2013', '2012', '2011', '2010')
GROUP BY parcel
HAVING due LIKE '%[0-9]%';
This query will do the trick :
SELECT ID,
CAST(REPLACE(REPLACE(IFNULL(Amount,0),',',''),'$','') AS DECIMAL(10,2))
FROM Table1
WHERE CAST(REPLACE(REPLACE(IFNULL(Amount,0),',',''),'$','') AS DECIMAL(10,2)) > 0
See SQLFIDDLE : http://www.sqlfiddle.com/#!2/55f433/1/0

Turn off scientific notation MySQL

When I insert a DOUBLE, why do I see a value like 9.755046187483832e17 when I select that value? How can I retrieve a number like 975504618748383289 instead?
You're probably looking for the FORMAT or ROUND function:
Using FORMAT(), depending on your locale and your specific needs, you might have to replace the thousands-separator:
mysql> SELECT FORMAT(9.755046187483832e17,0);
975,504,618,748,383,200
mysql> SELECT REPLACE(FORMAT(9.755046187483832e17,0), ',','');
975504618748383200
On the other hand, ROUND() being a numeric function, it only outputs digits:
mysql> SELECT ROUND(9.755046187483832e17,0);
975504618748383200
See http://sqlfiddle.com/#!2/d41d8/17614 for playing with that.
EDIT: As you noticed, the last two digits are rounded to 00. That's because of DOUBLE precision limits. You have to remember that double are approximate. If you need precise values and/or more digits than available with the 16-bits precision of double, you probably needs to change your column's type to DECIMAL. By default DECIMAL has 10 digits precision (10 base 10 digits). You could explicitly request up to 65 digits.
For example, if you need up to 20 digits precision, you write something like that:
CREATE TABLE tbl (myValue DECIMAL(20), ...
See http://dev.mysql.com/doc/refman/5.6/en/fixed-point-types.html
Please note however than things are not that simple. Selecting the decimal column might silently convert it to double (or bigint ?) thus loosing the extra precision. You might have to explicitly cast to string in order to preserve the full precision. That means the you might have to deal with that at application level.
create table tbl (dblValue DOUBLE, decValue DECIMAL(20,0));
insert into tbl values (975504618748383289, 975504618748383289);
SELECT dblValue, decValue FROM tbl;
--> DBLVALUE DECVALUE
--> 975504618748383200 975504618748383200
SELECT CAST(dblValue AS CHAR), CAST(decValue AS CHAR) FROM tbl;
--> CAST(DBLVALUE AS CHAR) CAST(DECVALUE AS CHAR)
--> 9.755046187483832e17 975504618748383289
See http://sqlfiddle.com/#!2/d5f58/2 for examples.
The double has a precision of about 16 digits. If you need more precision you have two options.
If the value is an integer, you can use bigint up to about 19 digits of precision.
Better is decimal which supports up to 65 digits. For instance, the following returns an error:
select format(v*v*v*v*v*v*v*v, 0)
from (select 1001 as v) t
Because the value of v is treated as a bigint.
However, the following works very nicely:
select format(v*v*v*v*v*v*v*v, 0)
from (select cast(1001 as decimal(65, 0)) as v) t
Returning 1,008,028,056,070,056,028,008,001 -- which is the precise answer.
If you need precision up to 65 places, then use decimal. Beyond that, you may have to write your own routines. If you are not doing arithmetic on the field, then consider storing it as a string.

SQL query for sorting numeric fields

I have one column of the type VARCHAR which stores numbers like 10, 11, 16.5, 24, 43, 12, 100, etc.
I want to order these fields but it sorts like this:
10
11
12
16.5
100
24
43
And I'd like this result:
10
11
12
16.5
24
43
100
How can I do this?
VARCHAR is a string type, so it's ordering them alphabetically, which is the expected behavior. If possible you should change the column to a number type. If that's not possible you can cast the value to a number like so:
SELECT ... ORDER BY CAST(column_name AS DECIMAL)
However, this will impact your performance if you have a lot of rows in your database.
order by to_number(mycol)
this will of course give an error if one value is not numeric. which begs the question - why not make it a numeric column in the first place.
You want to cast the field to be a numeric value instead, so that MySQL will order it with a numeric comparison, rather than a string comparison.
SELECT your_column
FROM your_table
ORDER BY CAST(your_column AS DECIMAL) ASC
The above will work with negative numbers too, if you have any of those in your table.
Although if the field only contains numeric data, you really should be storing it as such, rather than as a varchar. Having a cast in your query will dramatically affect performance as you get more rows.
Just add zero to your field without any cast or convert:
order by 0+yourfield ASC