3

I have a requirement where I need to convert all SQL Server stored procedures into HANA stored procedures. I have come across a function ISNUMERIC in T-SQL and I am not getting the equivalent of it in HANA.

After searching the web, I found that HANA does not have built in ISNUMERIC equivalent function. Then I tried writing my own function to achieve this and there I stuck with error handling and regular expression limitations.

My HANA version is 70.

3 Answers 3

3

SAP HANA does not come with a ISNUMERIC() function. However, this question had been asked and answered multiple times on SCN: E.g. http://scn.sap.com/thread/3449615

or my approach from back in the days: 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;

Testing this shows: with data as( select '1blablupp' as VAL from dummy 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   
1
  • 1
    This is not complete: what about -100, 1.000.000, etc. Nov 14, 2017 at 16:15
3

From SAP HANA 1.0 SPS12 you can use regular expressions in SQL. You can use LIKE_REGEXPR function to check if specific string contains letters i.e.:

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

This solution should cover really all strings that are convertable as a number:

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;

e.g. also covers: call "SCHEMA"."package::IS_NUMERIC"('-12.345', ?)

2
  • You're right, this very old solution didn't cover a huge range of cases. Partly this was due to the HANA limitations back in SP6/7. Your approach to actually performing the conversion and reporting success or failure is obviously correct.
    – Lars Br.
    Nov 15, 2017 at 2:38
  • 1
    However, often the requirements can be more specific (i.e. also ignoring commas, full stops and currency symbols like the T-SQL version does it). Also, the use of the procedure (why procedure for a single scalar value?) introduces quite some overhead. Alternatively one could use regex to have a close-enough check: MAP (locate_regexpr(START '[^0-9\-\.\,\+e]' FLAG 'i' in id_char), 0, 1, 0). Using this on 10 Mio rows took 8.5 secs while the procedure/function took 19.5 secs on my test system. What's best here really depends on the use case.
    – Lars Br.
    Nov 15, 2017 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.