
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Effect of Negative Value in MySQL TRUNCATE Function
If we specify the negative value of the second argument then the digits before the decimal point would be deleted without rounded off. The number of digits to be deleted depends upon the value of the negative second argument. Following examples will demonstrate the change, depending upon the negative value of the second argument, in the output of TRUNCATE() function.
mysql> Select TRUNCATE(1789.456,-1); +-----------------------+ | TRUNCATE(1789.456,-1) | +-----------------------+ | 1780 | +-----------------------+ 1 row in set (0.00 sec)
The query above returns 1780 because the first digit before the decimal point is deleted due to -1 value of the second argument.
mysql> Select TRUNCATE(1789.456,-2); +-----------------------+ | TRUNCATE(1789.456,-2) | +-----------------------+ | 1700 | +-----------------------+ 1 row in set (0.00 sec)
The query above returns 1700 because two digits before decimal point are deleted due to -2 value of the second argument.
mysql> Select TRUNCATE(1789.456,-3); +-----------------------+ | TRUNCATE(1789.456,-3) | +-----------------------+ | 1000 | +-----------------------+ 1 row in set (0.00 sec)
The query above returns 1000 because three digits before decimal point are deleted due to -3 value of the second argument.
mysql> Select TRUNCATE(1789.456,-4); +-----------------------+ | TRUNCATE(1789.456,-4) | +-----------------------+ | 0 | +-----------------------+ 1 row in set (0.00 sec)
The query above returns 0 because a total number of digits before the decimal point is 4 and the value of the second argument is -4.