Which function is SQL is used to truncate a number?

The TRUNCATE Function in MySQL is used to truncate a number to a specified number of decimal places.

Syntax :

TRUNCATE( X, D)

Parameter :

TRUNCATE() function accepts two parameters as mentioned above and described below.

  • X –The number which to be truncated.
  • D –Number of decimal places up to which the given number is to be truncated. If it is 0 it removes all the decimal values and returns only the integer. If it is negative, then the number is truncated to the left side of the decimal point.

Returns : It returns the number after truncated to the specified places.

Example:

Truncating a number when D is 0.

Truncating a Negative number –

SELECT TRUNCATE(-10.11, 0) AS Truncated_Number ;

Output :

+------------------+
| Truncated_Number |
+------------------+
|              -10 |
+------------------+

Truncating a Positive number –

SELECT TRUNCATE(100.61, 0) AS Truncated_Number ;

Output :

+------------------+
| Truncated_Number |
+------------------+
|              100 |
+------------------+