MYSQL Truncated incorrect DOUBLE value: Upon multiplying - mysql

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

Related

MySQL STR_TO_DATE Problem while using this function

As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo

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".

How do you round floats conditionally?

I am writing a query that is used by report generating software.
Part of this is querying for the hours needed to complete a project. We record this a 2 decimal float so that we can estimate to the quarter hour.
However, if we are using it in our report and the hour we recorded is something like 8.00, I want to query it and format it so that 8.00 is just 8. However any hours with something past the decimal, like 8.25, should remain as 8.25. How can I make this work?
hours Queried Result
====== -> My Query -> ==============
8.00 8
8.25 8.25
I am using MySQL 5.6
You can use the REPLACE() function to remove .00:
REPLACE(hours, '.00', '') AS hours
You can convert it to a string and check the rightmost 2 characters and trim those if they are '00'.
SELECT TRIM(TRAILING '.00' FROM CAST(column_name AS VARCHAR));
SELECT REPLACE(Round(8.00), '.00', ' ');
I will give more example so you can clear your Logic:
MySQL ROUND() rounds a number specified as an argument up to a number specified as another argument.
Syntax:
ROUND(N,[D]);
Where 'N' is rounded up to D decimal places.
and 'D' is indicating up to how many decimal places N will be rounded.
Example 1:-
SELECT ROUND(4.43);
Output :-
4
The above MySQL statement will round the given number 4.43. No decimal places have been defined, so the default decimal value is 0.
Example 2:-
SELECT ROUND(-4.53);
Output:-
-5
The above MySQL statement will round the given number -4.53. No decimal places have been defined, so the default decimal value is 0.

TINYTEXT Calculation in 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.

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