×

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places.

Syntax of MySQL Truncate Function

A number to a mentioned number of decimal places is truncated by the MYSQL TRUNCATE function, which is shown as follows

TRUNCATE ( Y, Z );

Description of above Syntax:

In the above-written syntax,

  • Y is a numeric expression or a literal number which is to be truncated.
  • Z is the number of decimal places to truncate to. The TRUNCATE function acts as Z digits left of the decimal point when the Z is negative. On the other hand, when the Z is zero, then it will return the value which has no decimal point.

But the important point is that Both Y and Z are needed.
There is a point to be noticed that in terms of reducing the decimal number, the TRUNCATE function is similar to the ROUND function. Although it is similar to that as the ROUND function rounds, the TRUNCATE function does not do that.

Application used for truncate() function:

The TRUNCATE() function can be used in the given below MySQL versions.

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Example of MySQL truncate function:

Now we will see some examples with the help of the TRUNCATE function.

1) Example of MySQL truncate function with a positive number of decimal places:

Let's visualize the example given below:

SELECT TRUNCATE ( 1.555, 1 );

Now the output is:

 TRUNCATE( 1.555, 1 )
 1.5

1 row in set (0.00 sec)

Due to the number of decimal places arguments being 1, so the MySQL TRUNCATE function keeps only 1 decimal place in the return value.

2) Example Of MySQL Truncate Function With A Negative Number Of Decimal Places:

The example given below shows us the example of the TRUNCATE function with a negative number of decimal places:

SELECT TRUNCTE ( 199.99 , -2 );

Now the output is:

 TRUNCATE( 199.99, -2 )
 100

1 row in set (0.00 sec)

3) MySQL Truncate() Vs. Round():

The below example takes the help of both TRUNCATE and ROUND functions for comparison:

SELECT
TRUNCATE (1.999, 1);
ROUND(1.999, 1) ;

Now the query output is:

 TRUNCATE( 1.999, 1 )ROUND( 1.999, 1)
 1.92.0

1 row in set (0.00 sec)

As we can see in the above output, the TRUNCATE function only trims the decimal places while the ROUND() function executes the rounding.

In the above context, we have learned how we can use the TRUNCATE function in MySQL to truncate a number to a mentioned number of the decimal place.


Related Topics

MySQL DAY() function

In this context, we will learn how we can use the MySQL DAY() function with proper syntax and good examples. Introduction of MySQL DAY() function This function in MySQL is used to...

2 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.