CONDENSE和SPLIT的HANA程序

时间:2016-10-16 13:29:56

标签: procedure hana

我正在尝试压缩并将字符串拆分为单行,例如

A     B C

A
B
C

到目前为止,以下过程适用于CALL Z_SPLITROW('A B C'),但如果我在字符之间有更多的空格则不行。有什么想法吗?

CREATE PROCEDURE Z_SPLITROW(TEXT nvarchar(100))
AS
BEGIN
  declare _items nvarchar(100) ARRAY;
  declare _text nvarchar(100);
  declare _index integer;
  _text := :TEXT;
  _index := 1;

  WHILE LOCATE(:_text,' ') > 0 DO
  _items[:_index] := SUBSTR_BEFORE(:_text,' ');
  _text := SUBSTR_AFTER(:_text,' ');
  _index := :_index + 1;
  END WHILE;
  _items[:_index] := :_text;

  rst = UNNEST(:_items) AS ("items");
  SELECT * FROM :rst;
END; 

1 个答案:

答案 0 :(得分:2)

要过滤掉灵活数量的空白字符,可以使用REPLACE_REGEXPR:

select 'A     B C' as orig,
        replace_regexpr ( '[[:space:]]+' 
                        IN  'A     B C'
                        WITH ' ') as repl
from dummy;


ORIG     |  REPL 
---------+-------
A     B C|  A B C