Search code examples
javamysqlsqlpercentagesqldatatypes

Putting precentage in MySQL database


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

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.