What is the usage of the SQL trunc function?

The TRUNC function in SQL is used to extract the integer part of a number. It can truncate a numerical expression to a specified number of decimal places and return it. The syntax of the TRUNC function is as follows:

Round down a number to a certain number of decimal places.

number is the numerical expression to be truncated, while decimal_places is the number of decimal places to be retained. If decimal_places is a positive number, the decimal portion of number is truncated to the specified number of places and the result is returned. If decimal_places is a negative number, the integer portion of number is truncated to the specified number of places and the result is returned.

Here are some examples of the TRUNC function:

TRUNC(3.14159, 2) gives the result of 3.14, keeping two decimal places;
TRUNC(3.14159, -1) gives the result of 3, truncating the integer part to the units digit;
TRUNC(12345.67, -3) gives the result of 12000, truncating the integer part to the thousands digit.

Please note that the TRUNC function does not round numbers, but simply truncates them. If rounding is needed, the ROUND function in SQL can be used.

bannerAds