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.
Related
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.
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
I need to store phone numbers starting with 0 but whenever i try to store this in MySql table the starting ZERO is removed because no number start with Zero actually
how i can solve this problem please do i need to change the field type from Integer to another type
Try to change the datatype to unsigned-zerofill. Refer the manual.
or you can use the LPAD function as well like
SELECT LPAD( yourNumber, lengthOfYourNumber, '0')
I got an error when i trying to save this number, I have referred mysql doc
But need know any other options to save a number which value is grater than 9223372036854775807.
Thanks
you can store it using a datatype called numeric
below is the workaround solution
You can store it as numeric(21,0) // 21 is the limit of number of digits
I have created a database table in mySQL of which two column names are "landPhone" and "mobilePhone" to store phone numbers (in the format of: 123-456-8000 for land and 098-765-6601 for mobile). These two columns' data type are set to VARCHAR(30). The data have been inserted in the table. But after SQL query, I found the phone numbers have been truncated. It shows (above two data for example) only first 3 digits (123) for landPhone and only first 2 digits after removing the leading '0' (98) for mobilePhone.
Why this is happening ?
Phone numbers are not actually numbers; they are strings that happen to contain digits (and, in your case, dashes). If you try to interpret one as a number, two things typically happen:
Leading zeros are forgotten.
Everything from the first non-digit to the end of the string is stripped off.
That sounds exactly like the result you're describing. Even if you end up stuffing the result into a string field, it's too late -- the data has already been corrupted.
Make sure you're not treating phone numbers as integers at any point in the process.
You must use
insert into sample values('123-456-8000', '098-765-6601' )
instead of
insert into sample values(123-456-8000, 098-765-6601 )
see this SQLFiddle.
Thanks all for your solution. As cHao suspected, it was me who did the mistake. When I first time created the table, I declared the datatype of the phone columns as INT, later I corrected them to VARCHAR().
When I dropped the table and inserted the same data to the new table, it is working fine.
That sounds exactly like the result you're describing. Even if you end up stuffing the result into a string field, it's too late -- the data has already been corrupted. ..cHao
Question to understand: Why mySQL doesn't override the previous datatype with the new one ?