【问题标题】:What is the equivalent of T-SQL ISNUMERIC function in HANA Sqlscript?HANA Sqlscript 中的 T-SQL ISNUMERIC 函数等价物是什么?
【发布时间】:2014-12-30 10:18:51
【问题描述】:

我需要将所有 SQL Server 存储过程转换为 HANA 存储过程。我在 T-SQL 中遇到了 ISNUMERIC 函数,但在 HANA 中没有得到它的等价物。

在网上搜索后发现HANA没有内置ISNUMERIC等效功能。然后我尝试编写自己的函数来实现这一点,但我遇到了错误处理和正则表达式限制。

我的 HANA 版本是 70。

【问题讨论】:

    标签: sql sql-scripts hana


    【解决方案1】:

    SAP HANA 没有 ISNUMERIC() 函数。 然而,这个问题已经在 SCN 上被多次询问和回答: 例如。 http://scn.sap.com/thread/3449615

    或者我过去的做法: http://scn.sap.com/thread/3638673

    drop function isnumeric;
    create function isNumeric( IN checkString NVARCHAR(64))
    returns isNumeric integer
    language SQLSCRIPT as
    begin
    declare tmp_string nvarchar(64) := :checkString;
    declare empty_string nvarchar(1) :='';
    
    /* replace all numbers with the empty string */
    tmp_string := replace (:tmp_string, '1', :empty_string);
    tmp_string := replace (:tmp_string, '2', :empty_string);
    tmp_string := replace (:tmp_string, '3', :empty_string);
    tmp_string := replace (:tmp_string, '4', :empty_string);
    tmp_string := replace (:tmp_string, '5', :empty_string);
    tmp_string := replace (:tmp_string, '6', :empty_string);
    tmp_string := replace (:tmp_string, '7', :empty_string);
    tmp_string := replace (:tmp_string, '8', :empty_string);
    tmp_string := replace (:tmp_string, '9', :empty_string);
    tmp_string := replace (:tmp_string, '0', :empty_string);
    
    /*if the remaining string is not empty, it must contain non-number characters */
    if length(:tmp_string)>0 then
        isNumeric := 0;
    else  
        isNumeric := 1;
    end if;
    
    end;
    

    测试表明: 数据为(从虚拟中选择'1blablupp'作为VAL union all select '1234' as VAL from dummy union all select 'bla123' as val from dummy)

    select val, isNumeric(val)  from data 
    
    VAL         ISNUMERIC(VAL)
    1blablupp   0            
    1234        1            
    bla123      0   
    

    【讨论】:

    • 这不完整:-100、1.000.000 等呢?
    【解决方案2】:

    从 SAP HANA 1.0 SPS12 开始,您可以在 SQL 中使用正则表达式。您可以使用LIKE_REGEXPR 函数来检查特定字符串是否包含字母,即:

    SELECT 
      CASE WHEN '0001A' LIKE_REGEXPR '[A-Z]' THEN 0 ELSE 1 END 
    FROM 
      DUMMY;
    

    【讨论】:

      【解决方案3】:

      这个解决方案应该真正涵盖所有可转换为数字的字符串

      PROCEDURE "SCHEMA"."package::IS_NUMERIC" ( 
              in stringToCheck nvarchar(5000)
              ,out isNumeric integer 
      ) 
          LANGUAGE SQLSCRIPT
          SQL SECURITY INVOKER 
          --DEFAULT SCHEMA <default_schema_name>
          READS SQL DATA AS
      BEGIN
      
      DECLARE EXIT HANDLER FOR SQLEXCEPTION
      BEGIN
          isNumeric := 0;
      END;
      
      select cast(:stringToCheck as double) from dummy; 
      
      isNumeric := 1;
      
      END;
      

      例如还涵盖call "SCHEMA"."package::IS_NUMERIC"('-12.345', ?)

      【讨论】:

      • 你是对的,这个非常古老的解决方案并没有涵盖大量的案例。部分原因是 SP6/7 中的 HANA 限制。您实际执行转换并报告成功或失败的方法显然是正确的。
      • 但是,通常要求可以更具体(即也像 T-SQL 版本一样忽略逗号、句号和货币符号)。此外,过程的使用(为什么要使用单个标量值的过程?)引入了相当多的开销。或者,可以使用正则表达式进行足够接近的检查:MAP (locate_regexpr(START '[^0-9\-\.\,\+e]' FLAG 'i' in id_char), 0, 1, 0)。在 10 个 Mio 行上使用它需要 8.5 秒,而在我的测试系统上使用过程/功能需要 19.5 秒。什么是最好的实际上取决于用例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2010-11-26
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2010-11-10
      • 2020-03-28
      相关资源
      最近更新 更多