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
In the TRUNCATE syntax, X is the integer number and D is the decimal points.
In the SQL, you can also use the TRUNCATE function with the integer field of the table as shown in the following block:
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 10.14544 by 3 decimal points:
Example 2: This example truncates the 908.5478 value by 2 decimal points and returns the truncated value in result:
Example 3: Here, you get the truncated value of 8.85478 by 3 decimal points:
Example 4: This example truncates the value of 252.54894 by 2 decimal points:
Example 5: In this example, we are going to perform the TRUNCATE function on the integer columns of the table.
So, first we will create the new table through which we will perform the TRUNCATE function.
The below CREATE query shows how to create the new table in the Structured Query Language:
Now, we create the new table Student_Sem_Marks using the above CREATE syntax which helps to store the marks of each semester of college students.
The following multiple INSERT queries inserts the records of students with names and their semester marks:
The following SELECT statement displays the inserted records of the above Student_Sem_Marks table:
Query 1: The following SELECT query uses the TRUNCATE function with the Total column of the above Student_Sem_Marks table:
This query truncates the total marks to 2 decimal points of each student.
Query 2: The following SELECT query uses the TRUNCATE function with the First_Sem and Second_Sem column of the above Student_Sem_Marks table:
This query truncates the values of first sem and second sem column to the 1 decimal point.