【问题标题】:Convert varchar to float将 varchar 转换为浮点数
【发布时间】:2017-05-16 23:27:10
【问题描述】:

在 SSMS 中,我有一个源表和一个目标表。在源表中,有一列通常包含十进制数字,但是,当输入值的用户输入 3,14 而不是 3.14 或“未知”等时,它偶尔会包含文本。(在源表中从技术上讲,用户可以输入他们想要的任何内容。此规则无法更改。)

我有一个存储过程,它从源表中挑选一些列,包括有问题的列,然后将信息输入数据仓库,以便我们对其进行分析。

我的目标是用空白或 -1 或其他一些 null 占位符替换/转换原始列中的 varchar 值。

我被告知要创建一个自定义函数来处理这个问题,但我不知道该怎么做,而且我在网上找到的文档现在已经超出了我的想象。

这是表格:

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(如果可能,最好使用自定义函数)?

【问题讨论】:

    标签: sql sql-server tsql sql-server-2016


    【解决方案1】:

    试试这个

     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
    

    【讨论】:

    • @john5 请检查。
    【解决方案2】:

    您可以使用try_cast(Column2 as float),如果它不能转换为float 数据类型,它将返回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

    返回:

    +----+------------+---------+
    | 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 及更高版本中:当转换失败而不是错误时,每个都将返回 null

    【讨论】:

      【解决方案3】:

      在用户输入各种数字类型的情况下,您可能可以使用ISNUMERIC

      如果值是数字,则返回 1,否则返回 0。您可能应该注意 1,303。如果您知道用户的意思是 1.303,那么您可以使用 try cast,但如果它实际上是一千……我不知道您如何保证您的假设。

      For more you can read here

      【讨论】:

      【解决方案4】:

      您可以使用 try_convert 或 case 如下

      Insert into Destination
      select Id, Column1, Case when ISNUMERIC(Replace(column2,',','.')) = 1 then convert(float, Replace(column2,',','.')) else -1 end 
          from SourceTable
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2022-01-17
      • 1970-01-01
      • 2013-03-21
      相关资源
      最近更新 更多