如何在SQL中将Varchar转换为Double?
您可以使用CAST()
函数将varchar转换为double 。语法如下:
SELECT yourColumnName1,yourColumnName2,......N, CAST(yourColumnName AS DECIMAL(TotalDigit,DigitAfterDecimalPoint)) anyVariableName FROM yourtableNameORDER BY anyVariableName DESC;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table VarcharToDouble-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> Name varchar(10),
-> Amount varchar(10) ,
-> PRIMARY KEY(Id)
-> );
使用insert命令在表中插入一些记录。查询如下:
mysql> insert into VarcharToDouble(Name,Amount) values('John','456.78');mysql> insert into VarcharToDouble(Name,Amount) values('Larry','8465.98');
mysql> insert into VarcharToDouble(Name,Amount) values('Mike','9083.45');
mysql> insert into VarcharToDouble(Name,Amount) values('Sam','46475.50');
mysql> insert into VarcharToDouble(Name,Amount) values('Carol','10003.35');
使用select语句显示表中的所有记录。查询如下:
mysql> select *from VarcharToDouble;
以下是输出:
+----+-------+----------+| Id | Name | Amount |
+----+-------+----------+
| 1 | John | 456.78 |
| 2 | Larry | 8465.98 |
| 3 | Mike | 9083.45 |
| 4 | Sam | 46475.50 |
| 5 | Carol | 10003.35 |
+----+-------+----------+
5 rows in set (0.00 sec)
这是使用以下命令将varchar转换为double的查询CAST()
:
mysql> select Id,Name, CAST(Amount AS DECIMAL(10,2)) TotalAmount from VarcharToDouble-> order by TotalAmount desc;
以下是输出:
mysql> select Id,Name, CAST(Amount AS DECIMAL(10,2)) TotalAmount from VarcharToDouble-> order by TotalAmount desc;
以上是 如何在SQL中将Varchar转换为Double? 的全部内容, 来源链接: utcz.com/z/348794.html