Syntax error Truncate results in decimal to integer value with MySQL

Truncate results in decimal to integer value with MySQL



Use truncate() for this, for example, 190.245 to 190. Following is the syntax −

select truncate(yourColumnName,0) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Value DECIMAL(10,4)
   -> );
Query OK, 0 rows affected (0.67 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(45.567);
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values(100.0000);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(15.89000);
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+----------+
| Value    |
+----------+
| 45.5670  |
| 100.0000 |
| 15.8900  |
+----------+
3 rows in set (0.00 sec)

Here is the query to truncate results from SQL to integer value.

mysql> select truncate(Value,0) from DemoTable;

Output

This will produce the following output −

+-------------------+
| truncate(Value,0) |
+-------------------+
| 45                |
| 100               |
| 15                |
+-------------------+
3 rows in set (0.00 sec)
Updated on: 2020-06-30T11:18:07+05:30

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements