0

使用 SQL Server 2000

表格1

id values (datatype is nvarchar)

001 12:10 
002 01:25
003 10:01
004 21:20

我想将值列转换为浮点数..

尝试查询

Select id, Cast(Replace(values, '':'', ''.'') as float) as values from table1

输出显示如下

001 12.1 'zero is not showing
002 01.25
003 10.01
004 21.2 'zero is not showing

为什么零在隐藏..?2.10 或 2.1 与 float 数据类型相同,但用户会混淆 2.1,例如 2 小时 1 分钟而不是 2 小时 10 分钟...

如何解决这个问题呢

预期产出

id values (datatype is float)

001 12.10 
002 01.25
003 10.01
004 21.20

需要 SQL 查询帮助

4

2 回答 2

2

12.10 and 12.1 are both string representations of the same float (twelve plus one tenth). If you want the data type to be float you cannot specify its string representation. You can only do that when converting a float into a string, which would be a pointless exercise in your case (since you already have this value as a string).

Another data type instead of float, such as decimal, which has a fixed number of decimal places, might be a more appropriate choice for your task.

于 2012-06-11T08:59:09.873 回答
0

尝试

Select id, Cast(Replace(values, '':'', ''.'') as DECIMAl(10,2)) as values from table1
于 2012-06-11T09:08:54.737 回答