How to Remove Trailing Zeros From a Decimal in SQL
To remove trailing zeros from a decimal in SQL, use theTRUNC() function. This function takes two arguments, the first being the decimal you want to truncate and the second being the number of decimal places you want to keep. For example, if you wanted to remove the trailing zeros from the decimal 1.2300, you would use the following query:
SELECT TRUNC(1.2300, 2) FROM table_name;
Examples
Let's look at a few examples of how this function can be used. Suppose you have a table called prices with the following data:
product | price |
---|---|
Apple | 1.2300 |
Orange | 2.4500 |
Banana | 3.6700 |
If you wanted to remove the trailing zeros from the prices in this table, you would use the following query:
SELECT product, TRUNC(price, 2) FROM prices;
This query would return the following result:
product | price |
---|---|
Apple | 1.23 |
Orange | 2.45 |
Banana | 3.67 |
Additional Info
The TRUNC() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database.