MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.