Convert to decimal in MySQL
Sometimes, we may need to convert one data type to another. Here is how we can DECIMAL(M,D)
convert to decimal in MySQL using CAST() and CONVERT() functions with .
Using CAST() with the DECIMAL(M,D) function to convert to decimal in MySQL
We can use CAST() function to convert one data type to another data type. It is usually used with HAVING, WHERE and JOIN clauses.
Sample code:
mysql> SELECT CAST(15 AS DECIMAL(4,2)) AS Decimal_Value;
Output:
+---------------+
| Decimal_Value |
+---------------+
| 15.00 |
+---------------+
1 row in set (0.00 sec)
Considering the query given above, we are converting int to decimal. Remember, we have to specify the precision and scale for conversion to decimal data type.
We used DECIMAL(M,D)
the function which takes two parameters. The first parameter is the precision and the second parameter is the scale.
Precision means the number of significant digits, while scale means the number of digits we can have (store) after the decimal point.
In the above example, 4 is the precision and 2 is the scale. We can also mention the column name instead of writing 15.
According to the MySQL documentation, when using FLOAT(M,D), DECIMAL(M,D) or DOUBLE(M,D), M must be greater than or equal to D. For example, in the following query, M is 2 and D is 4, which does not meet the condition M>=D and an error is reported.
mysql> SELECT CAST(15 AS DECIMAL(2,4)) AS Decimal_Value;
Always remember that the syntax of DECIMAL(M,0) is equivalent to DECIMAL(M). Likewise, the decimal syntax is equivalent to DECIMAL(M) and DECIMAL(M,0), where the default value of M is 10.
All of the following queries produce the same output.
Sample code:
mysql> SELECT CAST(15 AS DECIMAL(4,0)) AS Decimal_Value;
mysql> SELECT CAST(15 AS DECIMAL(4)) AS Decimal_Value;
mysql> SELECT CAST(15 AS DECIMAL) AS Decimal_Value;
Output (for all three queries given above):
+---------------+
| Decimal_Value |
+---------------+
| 15 |
+---------------+
1 row in set (0.00 sec)
Use CONVERT() with the DECIMAL(M,D) function to convert to decimal in MySQL
We can also use CONVERT() with the DECIMAL(M,D) function to convert an int to a decimal. This is similar to the CAST() function discussed above.
Sample code:
mysql> SELECT CONVERT(15, DECIMAL(4,2)) AS Decimal_Value;
Output:
+---------------+
| Decimal_Value |
+---------------+
| 15.00 |
+---------------+
1 row in set (0.00 sec)
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u