Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL TRUNCATE() function

TRUNCATE() function

MySQL TRUNCATE() returns a number after truncated to certain decimal places. The number and the number of decimal places are specified as arguments of the TRUNCATE function.

Syntax:

TRUNCATE(N, D);

Arguments:

Name Description
N A number which is to be truncated up to D decimal places.
D A number indicating up to how many decimal places, N is to be truncated.

Note: When the value of ‘D’ is 0 the results have no fractional part and the ‘D’ digits left of the decimal point of the value ‘N’ become 0 when the value of ‘D’ is negative.

Syntax Diagram:

MySQL TRUNCATE() Function - Syntax Diagram

MySQL Version: 5.6


Example of MySQL TRUNCATE() function

Code:

SELECT TRUNCATE(2.465,1);

Explanation:

The above MySQL statement will return a value truncating 2.465 up to 1 decimal place.

Sample Output:

mysql> SELECT TRUNCATE(2.465,1);
+-------------------+
| TRUNCATE(2.465,1) |
+-------------------+
|               2.4 | 
+-------------------+
1 row in set (0.00 sec)

Example :TRUNCATE() function with negative decimal places

Code:

SELECT TRUNCATE(142.465,-2);

Explanation:

The above MySQL statement will return a value truncating 142.465 up to -2 decimal places.

Sample Output:

mysql> SELECT TRUNCATE(142.465,-2);
+----------------------+
| TRUNCATE(142.465,-2) |
+----------------------+
|                  100 | 
+----------------------+
1 row in set (0.00 sec)

All Mathematical Functions

MySQL Mathematical Functions, slide presentation

Previous: TAN()
Next: FORMAT()