将 varchar 转换为 float
Convert varchar to float
在 SSMS 中,我有一个源 table 和一个目标 table。在源 table 中,有一列通常包含十进制数字,但是,当输入值的用户输入 3,14 而不是 3.14 或 "unknown" 等时,它偶尔会包含文本.(在来源 table 中,技术上允许用户输入他们想要的任何内容。此规则无法更改。)
我有一个存储过程,它从源 table 中挑选一些列,包括有问题的列,并将信息输入数据仓库,以便我们对其进行分析。
我的目标是 replace/convert 原始列中的 varchar 值带有空白或 -1,或其他一些空值占位符。
我被告知要创建一个自定义函数来处理这个问题,但我不知道该怎么做,而且我在网上找到的文档在这一点上让我头疼。
以下是 table:
create table SourceTable (
id INT,
Column1 DATE,
Column2 VARCHAR(50)
);
insert into SourceTable (id, Column1, Column2) values (1, '5/8/2017', '533');
insert into SourceTable (id, Column1, Column2) values (2, '10/1/2016', '988');
insert into SourceTable (id, Column1, Column2) values (3, '2/8/2016', '411');
insert into SourceTable (id, Column1, Column2) values (4, '2/29/2016', '491');
insert into SourceTable (id, Column1, Column2) values (5, '3/15/2016', '500');
insert into SourceTable (id, Column1, Column2) values (6, '4/2/2017', '677');
insert into SourceTable (id, Column1, Column2) values (7, '5/4/2016', '56/58');
insert into SourceTable (id, Column1, Column2) values (8, '8/24/2016', 'Unknown');
insert into SourceTable (id, Column1, Column2) values (9, '2/2/2017', '');
insert into SourceTable (id, Column1, Column2) values (10, '1/7/2017', '410');
create table Destination (
id INT,
Column1 DATE,
Column2 float
);
如何将 SourceTable.Column2 中的数字导入 Destination.Column2(如果可能最好使用自定义函数)?
您可以使用 try_cast(Column2 as float)
,如果它不能转换为 float
数据类型,它将 return null
。
如果你想用你建议的占位符值替换 null
,你可以使用 isnull()
或 coalesce()
insert into destination (id, column1, column2)
select id, column1, coalesce(try_cast(column2 as float),-1)
from sourcetable
rextester 演示:http://rextester.com/FJTZ50419
returns:
+----+------------+---------+
| id | column1 | column2 |
+----+------------+---------+
| 1 | 2017-05-08 | 533 |
| 2 | 2016-10-01 | 988 |
| 3 | 2016-02-08 | 411 |
| 4 | 2016-02-29 | 491 |
| 5 | 2016-03-15 | 500 |
| 6 | 2017-04-02 | 677 |
| 7 | 2016-05-04 | -1 |
| 8 | 2016-08-24 | -1 |
| 9 | 2017-02-02 | 0 |
| 10 | 2017-01-07 | 410 |
+----+------------+---------+
在 Sql Server 2012 及更高版本中:当转换失败而不是错误时,其中每一个都会 return null
。
在用户输入各种数字类型的情况下,您可能会使用 ISNUMERIC
。
这将 return 如果值为数字则为 1,否则为 0。您可能应该注意 1,303。如果你知道用户的意思是 1.303 那么你可以使用 try cast,但如果它实际上是一千......我不知道你如何保证你的假设。
您可以使用try_convert或如下大小写
Insert into Destination
select Id, Column1, Case when ISNUMERIC(Replace(column2,',','.')) = 1 then convert(float, Replace(column2,',','.')) else -1 end
from SourceTable
试试这个
Create FUNCTION dbo.FindNoneNumeric
(@FloatString VARCHAR(8000))
RETURNS INT
AS
BEGIN
Declare @Status int
SET @Status =
(SELECT CASE
WHEN @FloatString NOT LIKE '%[^0-9]%'
THEN 1
ELSE 0
END)
return @Status
END
插入语句
insert into Destination(ID,Column1,Column2)
Select ID,Column1,Column2 from SourceTable
where dbo.FindNoneNumeric(SourceTable.Column2)=1
在 SSMS 中,我有一个源 table 和一个目标 table。在源 table 中,有一列通常包含十进制数字,但是,当输入值的用户输入 3,14 而不是 3.14 或 "unknown" 等时,它偶尔会包含文本.(在来源 table 中,技术上允许用户输入他们想要的任何内容。此规则无法更改。)
我有一个存储过程,它从源 table 中挑选一些列,包括有问题的列,并将信息输入数据仓库,以便我们对其进行分析。
我的目标是 replace/convert 原始列中的 varchar 值带有空白或 -1,或其他一些空值占位符。
我被告知要创建一个自定义函数来处理这个问题,但我不知道该怎么做,而且我在网上找到的文档在这一点上让我头疼。
以下是 table:
create table SourceTable (
id INT,
Column1 DATE,
Column2 VARCHAR(50)
);
insert into SourceTable (id, Column1, Column2) values (1, '5/8/2017', '533');
insert into SourceTable (id, Column1, Column2) values (2, '10/1/2016', '988');
insert into SourceTable (id, Column1, Column2) values (3, '2/8/2016', '411');
insert into SourceTable (id, Column1, Column2) values (4, '2/29/2016', '491');
insert into SourceTable (id, Column1, Column2) values (5, '3/15/2016', '500');
insert into SourceTable (id, Column1, Column2) values (6, '4/2/2017', '677');
insert into SourceTable (id, Column1, Column2) values (7, '5/4/2016', '56/58');
insert into SourceTable (id, Column1, Column2) values (8, '8/24/2016', 'Unknown');
insert into SourceTable (id, Column1, Column2) values (9, '2/2/2017', '');
insert into SourceTable (id, Column1, Column2) values (10, '1/7/2017', '410');
create table Destination (
id INT,
Column1 DATE,
Column2 float
);
如何将 SourceTable.Column2 中的数字导入 Destination.Column2(如果可能最好使用自定义函数)?
您可以使用 try_cast(Column2 as float)
,如果它不能转换为 float
数据类型,它将 return null
。
如果你想用你建议的占位符值替换 null
,你可以使用 isnull()
或 coalesce()
insert into destination (id, column1, column2)
select id, column1, coalesce(try_cast(column2 as float),-1)
from sourcetable
rextester 演示:http://rextester.com/FJTZ50419
returns:
+----+------------+---------+
| id | column1 | column2 |
+----+------------+---------+
| 1 | 2017-05-08 | 533 |
| 2 | 2016-10-01 | 988 |
| 3 | 2016-02-08 | 411 |
| 4 | 2016-02-29 | 491 |
| 5 | 2016-03-15 | 500 |
| 6 | 2017-04-02 | 677 |
| 7 | 2016-05-04 | -1 |
| 8 | 2016-08-24 | -1 |
| 9 | 2017-02-02 | 0 |
| 10 | 2017-01-07 | 410 |
+----+------------+---------+
在 Sql Server 2012 及更高版本中:当转换失败而不是错误时,其中每一个都会 return null
。
在用户输入各种数字类型的情况下,您可能会使用 ISNUMERIC
。
这将 return 如果值为数字则为 1,否则为 0。您可能应该注意 1,303。如果你知道用户的意思是 1.303 那么你可以使用 try cast,但如果它实际上是一千......我不知道你如何保证你的假设。
您可以使用try_convert或如下大小写
Insert into Destination
select Id, Column1, Case when ISNUMERIC(Replace(column2,',','.')) = 1 then convert(float, Replace(column2,',','.')) else -1 end
from SourceTable
试试这个
Create FUNCTION dbo.FindNoneNumeric
(@FloatString VARCHAR(8000))
RETURNS INT
AS
BEGIN
Declare @Status int
SET @Status =
(SELECT CASE
WHEN @FloatString NOT LIKE '%[^0-9]%'
THEN 1
ELSE 0
END)
return @Status
END
插入语句
insert into Destination(ID,Column1,Column2)
Select ID,Column1,Column2 from SourceTable
where dbo.FindNoneNumeric(SourceTable.Column2)=1