
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to store decimal in MySQL?
To store decimal in MySQL, you need to understand the two parameters. The syntax is as follows −
DECIMAL(yourTotalDigit,yourDigitsAfterDecimalPoint);
For Example −
DECIMAL(4,2), it means you can take 4 digits total and 2 digit after decimal point.
The first parameter is up to 2 before the decimal point
The second parameter is up to 2 digit after decimal point.
- Case 1 − 12.34 is valid.
- Case 2 − 123.4 is not valid.
- Case 3 − 1.234 is valid because the value 4 will be ignored and it will be considered 1.23
Now you can check using table −
mysql> create table DecimalDemo -> ( -> Amount DECIMAL(4,2) -> ); Query OK, 0 rows affected (0.47 sec)
The invalid values for our example Decimal(4,2) are as follows −
mysql> insert into DecimalDemo values(123.4); ERROR 1264 (22003): Out of range value for column 'Amount' at row 1 mysql> insert into DecimalDemo values(1234); ERROR 1264 (22003): Out of range value for column 'Amount' at row 1 mysql> insert into DecimalDemo values(1234.56); ERROR 1264 (22003): Out of range value for column 'Amount' at row 1
The valid values are as follows −
mysql> insert into DecimalDemo values(12.34); Query OK, 1 row affected (0.13 sec) mysql> insert into DecimalDemo values(12.4); Query OK, 1 row affected (0.18 sec) mysql> insert into DecimalDemo values(.2345); Query OK, 1 row affected, 1 warning (0.18 sec) mysql> insert into DecimalDemo values(1.234); Query OK, 1 row affected, 1 warning (0.16 sec)
Display all the valid values from the table using select statement. The query is as follows −
mysql> select *from DecimalDemo;
Output
+--------+ | Amount | +--------+ | 12.34 | | 12.40 | | 0.23 | | 1.23 | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to store the PayPal decimal amount in the MySQL database?
- How do I format a number as decimal to store it in MySQL?
- How to store decimal value in Android sqlite?
- How to store decimal values in a table using PreparedStatement in JDBC?
- How to store data in MySQL as JSON?
- How to use MySQL decimal?
- How to store time created in a MySQL table?
- How to store usernames and passwords safely in MySQL database?
- How to store Query Result in a variable using MySQL?
- How to insert DECIMAL into MySQL database?
- How can we allow MySQL to store invalid dates?
- How to format number to 2 decimal places in MySQL?
- BOOLEAN or TINYINT to store values in MySQL?
- Best way to store weekly event in MySQL?
- MySQL - CAST DECIMAL to INT?

Advertisements