×

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

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 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 Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 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.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 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 Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.