×

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 MONTHNAME() function

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

3 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

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 Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 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 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 Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL INSERT() Function

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

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL POW() function

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

2 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 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 ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL PERIOD_ADD() function

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

2 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.