MYSQL database Store Float Number as 00.00 - mysql

I have a column in MYSQL database table called available_balance float(10,2), Its all fine but its storing value 0.00 instead of 00.00 or if 09.33 its storing 9.33. Instead I am looking for 09.33 to store, I can display 09.33 using PHP but since I have large data, I do not want process to convert numbers in PHP if mysql database with Float Column allow me to store number like 00.00 and 09.33 etc.
Let me know if anyone here can help me for solve the puzzle. Thanks!

You can display it with the format you need but not store it with float column type,
Use function LPAD() to show the numbers (left) padded with zeros:
select id, LPAD(available_balance,5,"0") from my_table
result
id LPAD(available_balance,5,"0")
1 05.00
2 50.20
3 01.20

Related

How to determine the right datatype for columns

Please look at my screenshots and help me to understand what I am missing.
What datatype should I choose for these columns in MYSQL? I keep getting mistakes in decimal datatype columns. I chose decimаl12,3 because no columns(revenue, product&purchase price) with currency have more than 12 digits in total, 9 before and 3 after the decimal point. Could someone help me to understand what data type to choose with examples?
if we have an integer number e.g. 85192 we choose int?
for currency we choose the decimal, right? then what have I done wrong that I keep getting errors? 0 records imported.
if we have a combination of numbers and letters or just letters then we choose varchar? and varchаr1 equals 1 character, eg. apple32 = 7 characters, therefore vаrchar7?
turning to decimal, 12,464.87 in total 7 digits, 5 before and 2 after the decimal point, hence mysql decimаl7,2 should be enough, right? or would it be better to put decimаl10,3 with a margin so to say.
excel
mysql
data
$1,000.00 contains two characters that cannot be part of a numeric literal: the dollar sign and the comma that is used as a thousands separator.
Find a way to change '$1,000.00' to '1000.00' in the input file. Then, the load will succeed.
Alternatively, create an intermediate table where product_price is a VARCHAR(32), load into that, and then:
INSERT INTO target_table
SELECT
other_col1
,other_col2
, ....
,CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2)
,other_col_n
,...
FROM staging_table;
You don't need an intermediate table. When doing LOAD DATA, put and columns into #variables; then use a SET to convert as needed:
LOAD DATA
...
col1, col2, #price, ...,
SET price = CAST(REPLACE(REPLACE(product_price,',',''),'$','') AS DECIMAL(15,2))
Dates need to be like this: "2022-07-25 22:02:22". Either change what Excel is delivering, or use STR_TO_DATE(...) in the SET.

SUM() function gives incorrect output

I have a single table named trust_fund:
trust_fund table
My Query is:
$sql = "SELECT SUM(available_balance) AS total_fund FROM trust_funds WHERE type = 'Admin'";
at first, it displays 100000000 which is correct, but when I update the table (transfered 500 from Admin to Agent), Admin available_balance become 99,999,504 and Agent becomes 500.
There is an extra 4 in Admin Balance. I tried transfering the 500 back to Admin Account and Admin Account becomes 100000008.
The Output of the Query
I am confused because the tables shows correct values, 100000000 and 0, but running the query will give 100000004.
Please help
Table Definition
Column
Type
id
INT(AI)
uid
Text
available_balance
Float
wallet_address
Text
type
Text
Your problem arises from your use of the FLOAT data type for currency.
FLOAT is a poor choice for a currency column because it attracts errors from representation and rounding (See Is floating point math broken?)
Use INT (or BIGINT) if you're working with integer values. Alternatively, use DECIMAL to store and work with exact decimal values.

Remove trailing decimal values to 1 place in sql server 2008 with money format

I have number in DB column and i want to format this with money,decimal data types.
my query is like below,
SELECT CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))
AS money),1) AS HouseholdGoodsInsuredAmount FROM Table
HouseholdCoverage = 1175012
My query returns the value like below,
97,917.70
but i want result as
97,917.7 (only 1 number after decimal places).
If i cast the value as float i am getting error.
I know i can use parsename to separate the values after decimal place and add it again with few modifications. But that is not good i feel.
As well as i know this has to be done in C# application, but this query is already written in sql server 2008, i just want modify it according to requirements.
I just figured it out myself after doing some extensive research.. but dont know performance will be good or bad
SELECT LEFT(CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))AS money),1),
len(CONVERT(VARCHAR,CAST(CAST(CAST(HouseholdCoverage AS DECIMAL(10,1))/ 12 AS DECIMAL(10,1))AS money),1))-1)
AS HouseholdGoodsInsuredAmount FROM Table
Now the result is as i expected with 1 decimal places after decimal point,
97,917.7
Just keep is simple. Divide your number by a decimal number and cast the answer as DECIMAL(10,1)
DECLARE #HouseholdCoverage BIGINT = 1175012
SELECT CAST(#HouseholdCoverage/128.0 AS DECIMAL(10,1))
DECIMAL(10,1) will make sure that there will be only 1 number after precision(.)

Copying double value from a column of a table into another string column with fixed lenth pattern in MySql

I am using MySql and came across a problem.
I have a column name price(Double) and i want to create a new column in the same table of datatype String and wants to copy the price into new column for all rows present in the table
with fixed length pattern.
example i want to do the following
Price(double) newcolumn(String)
12 00012
1 00001
0 00000
please help me to sort out this problem.
Perhaps something like this could work:
UPDATE YourTable SET newcolumn = LPAD(REPLACE(FORMAT(Price,0),',',''),5,'0')
The FORMAT function converts the double into a string with 0 decimals and here is the api.
The FORMAT introduces commas to separate thousands so we need to use REPLACE to get rid of those and here is the api.
The LPAD function left pads with the '0' and here is the api

MySQL - storing numbers with decimal point

exist any way to store in database a numbers with or without decimal point? The user can store to database numbers like 10 and 10.1 - for storing numbers in MySQL I use data type column decimal, so for this example, in the database will be stored 10.00 and 10.10.
But I will need to store the exact form of number by user - so in this case 0 10 and 10.1.
Is possible to solve it on database level or I have to edit on application level?
make use on case
case format(#number,0)=#number
when 1 then format(#number,0)
else
case format(#number,1)=#number
when 1 then format(#number,1)
else #number
end
end
or if
if (format(#number,0)=#number, format(#number,0),
if (format(#number,1)=#number, format(#number,1), #number))
one way is when you make the select query to omit this leading zero
http://www.foscode.com/remove-trailing-zeros-mysql-decimal-fields/
You could just save whatever the user entered as a VARCHAR, so you'll store exactly what they entered. Then when you need it, you can load it from the DB, and parse it to the corresponding float value as needed.