我有一个包含一些数据的varchar字段,我需要转换这些值并将其插入float数据类型字段
create table #tableTest
(
someData varchar(100) not null
);
insert into #tableTest(someData) values('5 215 243');
create table #tableFloat
(
floatData float not null
);
insert into #tableFloat
select someData from #tableTest;
我尝试过这个:
insert into #tableFloat
select replace(someData, ' ', '')
from #tableTest
除了要成为表#tableFloat的结果之外,我的值都是5215243.00,方法是删除它们之间的空格
答案 0 :(得分:1)
尝试一下
CREATE TABLE #tabletest
(
somedata VARCHAR(100) NOT NULL
);
INSERT INTO #tabletest
(somedata)
VALUES ('5 215 243');
CREATE TABLE #tablefloat
(
FLoatdata FLOAT
);
INSERT INTO #tablefloat
SELECT LTRIM(RTRIM(REPLACE(somedata, ' ', ''))) AS someData --Replace space values and appy trim function to remove spaces either side
FROM #tabletest;
SELECT CAST(CAST(CAST(FLoatdata AS INT) AS VARCHAR(20))+'.00' AS DECIMAL(20,2)) FloatToDecmialData -- display the value of float datatype by converting in to decimal
FROM #tablefloat
结果
data
----
5215243.00
答案 1 :(得分:1)
我知道已经解决了这个问题,但这是我的简化解决方案(只需一个CAST,无需修整):
makeP()