使用 MySQL 將十進位制結果截斷為整數


使用 truncate() 完成此操作,例如,將 190.245 截斷為 190。以下是語法 −

select truncate(yourColumnName,0) from yourTableName;

我們首先建立一個表 −

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

使用 insert 命令在表中插入一些記錄 −

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)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

輸出

將生成以下輸出 −

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

以下是將 SQL 結果截斷為整數值的查詢。

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

輸出

將生成以下輸出 −

+-------------------+
| truncate(Value,0) |
+-------------------+
| 45                |
| 100               |
| 15                |
+-------------------+
3 rows in set (0.00 sec)

更新時間: 30-Jun-2020

476 次瀏覽

開啟你的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.