×

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points.

Syntax of TRUNCATE Function

SELECT TRUNCATE(X, D) AS Alias_Name;

In the TRUNCATE syntax, X is the integer number and D is the decimal points.

In the Structured Query Language, we can also use the TRUNCATE function with the columns of the table as shown in the following block:

SELECT TRUNCATE(Column_Name1, Column_Name2) AS Alias_Name FROM Table_Name;

In this syntax, we have to define the name and columns of that table on which we want to perform the TRUNCATE function. 

Examples of TRUNCATE function

Example 1: This example gets the truncate value by truncated 101 by 4:

SELECT TRUNCATE(10.14544, 4) AS Truncate_of_101by4;

Output:

Truncate_of_10.14544by4
101.1454

Example 2: This example divides 0.0145 by 4 and returns the truncate value in result:

SELECT TRUNCATE(0.0145, 2) AS Truncate_of_0.0145by2;

Output:

Truncate_of_0.0145 by2
0.01

Example 3: This example divides 8 by 5 and returns the truncate value in result:

SELECT TRUNCATE(0, 5) AS Truncate_of_0by5;

Output:

Truncate_of_0by5
0

Example 4: This example divides 255 by 200 and returns the truncate value in result:

SELECT TRUNCATE(255, 200) AS Truncate_of_255by200;

Output:

Truncate_of_255by200
55

Example 5: This example uses the TRUNCATE function with the SQL table.

In this example, we will create the new table through which we will perform the TRUNCATE function on the columns of the table:

The following shows the syntax to create the new table in SQL:

CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),  
Second_Column_of_table Data Type (character_size of the Second column ),  
Third_Column_of_table Data Type (character_size of the Third column),  
…….,  
Last_Column_of_table Data Type (character_size of the Last column)
);  

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

CREATE TABLE Product_Details
(
Product_ID INT NOT NULL,
Product_Name Varchar(50),
Product_Quantity INT,
Purchasing_Price INT,
Selling_Price INT,
Release_Date Date, 
Product_Rating INT
);


The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (104, P1, 10, 945, NULL, 2022-04-30, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15, 45, 75, 2022-01-28, 5);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (103, P2, 18, 25, NULL, 2022-02-18, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (111, P7, 25, 5, 15, 2021-12-25, 9);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (210, P6, 15, 50, 70, 2021-10-15, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19, 110, 250, 2022-01-28, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 10, 550, 835, 2022-04-11, NULL);

The following SELECT statement displays the inserted records of the above Product_Details table:

SELECT * FROM Product_Details; 
Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110945NULL2022-04-30NULL
202P41545752022-01-285.9858
103P21825NULL2022-02-1842.85789
111P7255152021-12-259
210P61550702021-10-15NULL
212P8191102502022-01-284
112P10105508352022-04-11NULL

Query 1: The following SELECT query uses the TRUNCATE function with the Product_Quantity column of the above Product_Details table:

SELECT Product_ID, TRUNCATE(Product_ID, 1) AS Truncate_of_ProductID_by100 FROM Product_Details;

This query truncated each product_id by 1 decimal point and returns the value after truncated.

Output:

Product_IDTruncate_of_ProductID_by1
104.582104.5
202.685202.6
103.935103.9
111.985111.9
210.2548210.2
212.258212.2
112.3657112.3

Query 2: The following SELECT query uses the TRUNCATE function with the and Purchasing_Price and Selling_Price column of the above Product_Details table:

SELECT Purchasing_Price, Product_Quantity, TRUNCATE(Purchasing_Price, 2) AS Truncate_ofpurhcaseprice, Selling_Price, Product_Quantity, TRUNCATE(Selling_Price, 2) AS Truncate_of_SellingPrice FROM Product_Details;

This query truncates the purchasing price and selling price of each product by 2 decimal points and returns the truncated value.

Output:

Purchasing_Price Truncate_ofpurhcasepriceSelling_Price Truncate_ofsellingprice
945.2945.00NULL2 
45245.0075275.00
25225.00NULL2 
525.0015215.00
50250.070270.00
1102110.002502250.00
5502550.008352835.00


Query 3: The following SELECT query uses the TRUNCATE function with the Product_Rating column of the above Product_Details table:

SELECT TRUNCATE(Product_Rating, 2) AS Truncate_ofratingby2 FROM Product_Details;

This query truncates each rating of product by 2 decimal points and returns the truncated value.

Output:

Product_RatingTruncate_ofratingby2
NULL-
5.98585.98
42.8578942.85
99.00
NULL-
44.00
NULL-

Related Topics

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.