Question

Percentage Table

I have some code which adds entries from a .csv file to the MySQL database. I would like to know what datatype to use to add the percentage from the .csv file to the percentage column in the MySQL table. I tried with decimal (Though I knew that was wrong.), and I got this error.

java.sql.BatchUpdateException: Incorrect decimal value: '8%' for column 'Percentage' at row 1
Was it helpful?

Solution

if you want to store like 80% then the datatype should be varchar or If you just want to put 80.11 then the datatype is decimal which you are using.

I would suggest you keep the datatype as it is(decimal) because after retrieving from Database if you want some calculations then it will be extra burden for you. If it would be varchar then while retrieving it you have to keep in the string in java.Then you have to remove % then convert into float then calculations.

OTHER TIPS

Decimal would work, but you would just store a value of 8 not '8%' which would need to be a varchar (string).

If you have the option, I'd encourage you to store it as a decimal (remove the percentage character) and then format the number to show it with a percentage sign after retrieving it from the database. In that case, you can still use it as a decimal data type, else you'd have to use a string-based one, like varchar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top