Sql 哈娜:分裂字符串?
有没有办法在HANA中拆分字符串 类似于SQL Server中的等效项:从dbo.fnSplitString'valueA,valueB',','中选择*Sql 哈娜:分裂字符串?,sql,hana,Sql,Hana,有没有办法在HANA中拆分字符串 类似于SQL Server中的等效项:从dbo.fnSplitString'valueA,valueB',','中选择* 拆分字符串的另一种方法是使用表类型使用出站变量: CREATE TYPE UTIL_VARCHAR_LIST AS TABLE ( VarcharValue Varchar(5000) ); CREATE PROCEDURE UTIL_SPLIT_STRING ( IN iv_split_string Varchar(500
拆分字符串的另一种方法是使用表类型使用出站变量:
CREATE TYPE UTIL_VARCHAR_LIST AS TABLE
(
VarcharValue Varchar(5000)
);
CREATE PROCEDURE UTIL_SPLIT_STRING
(
IN iv_split_string Varchar(5000),
IN iv_split_character Varchar(1) DEFAULT ',',
OUT ot_string_list UTIL_VARCHAR_LIST
) LANGUAGE SQLSCRIPT
AS
BEGIN
DECLARE TEMP_STR VARCHAR(5000) := :iv_split_string || :iv_split_character;
DECLARE OUT_VAR VARCHAR(5000) ARRAY;
DECLARE POS INTEGER :=1;
DECLARE FLAG INTEGER := 1;
DECLARE LEFT_STR VARCHAR(5000);
WHILE(LENGTH(:TEMP_STR) > 0 )
DO
LEFT_STR := SUBSTR_BEFORE (:TEMP_STR,:iv_split_character);
TEMP_STR := SUBSTR_AFTER (:TEMP_STR,:LEFT_STR || :iv_split_character);
OUT_VAR[POS] := LEFT_STR;
POS := :POS + 1;
END WHILE;
ot_string_list = UNNEST(:OUT_VAR) AS ("VARCHARVALUE");
END;
在SP10之前,您需要创建一个函数来拆分字符串 如果需要获取一些字符,可以使用SUBSTR 在SP9或更高版本上,可以使用正则表达式
我知道这个问题有点老了,但我从来没有找到一个好的解决办法。 所以我为此创建了一个表UDF,我认为这比为此调用过程要好,因为您不需要创建额外的变量。 好的,这是代码,希望它有帮助 这归功于佩德·赖斯,因为我只修改了他的代码。干杯
FUNCTION "TF_SPLIT_STRING"
(
i_string nvarchar(255)
)
RETURNS table(type char(1), value nvarchar(255))
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
--> DECLARATIONS
-- The remaining string after checking for the delimiter
declare v_remaining VARCHAR(256) := :i_string;
-- The current vale that is being handled
declare v_current VARCHAR(255) := '';
-- The current positions of the resulting arrays
declare v_pos_include INTEGER := 1;
declare v_pos_exclude INTEGER := 1;
-- The arrays containing the results
declare a_include VARCHAR(12) array := ARRAY(0);
declare a_exclude VARCHAR(12) array := ARRAY(0);
-- The delimiter and the excluder
declare v_delimiter char(1) := ',';
declare v_excluder char(1) := '-';
--> SETUP BLOCK
if substr(:v_remaining, length(:v_remaining), 1) <> :v_delimiter
then
v_remaining := :v_remaining || :v_delimiter;
end if;
--> EXECUTION BLOCK
-- While there is a string to process
-- -> Split the string into the current value and the remaining
-- -> Check if there is an excluder character in the current value
-- -> If an excluder character is found, push the current value,
-- without the excluder, to the resulting array of excluding values
-- -> If no excluder is found, push the value to the resulting array
-- of including values
-- -> The corresponding position variable is incremented accordingly
while( LENGTH(:v_remaining) > 0 )
do
v_current := SUBSTR_BEFORE (:v_remaining,:v_delimiter);
v_remaining := SUBSTR_AFTER (:v_remaining, :v_current || :v_delimiter);
if substring(:v_current, 1, 1) = :v_excluder
then
a_exclude[v_pos_exclude] := SUBSTR_AFTER (:v_current, :v_excluder);
v_pos_exclude := :v_pos_exclude + 1;
else
a_include[v_pos_include] := v_current;
v_pos_include := :v_pos_include + 1;
end if;
end while;
--> ASSIGNMENTS BLOCK
-- format counter and divisions as table
o_include = unnest(:a_include) AS ("VALUE");
o_exclude = unnest(:a_exclude) AS ("VALUE");
return
select 'I' as type, value from :o_include
union all
select 'E' as type, value from :o_exclude
;
END;
使用SUBSTR_REGEXPR函数的解决方案。我上周在Hana中将此用作脚本视图,可以确认它是否有效
Begin
VAR_OUT =
select *
from ( select
ST.PARAM, -- Source table id
NT.Element_Number, -- Occurrence number within source table row
SUBSTR_REGEXPR( '([;])([^;]*)(?=;)'
IN CONCAT(CONCAT(';',ST.VALUE),';')
OCCURRENCE NT.Element_Number
GROUP 2
) splitted -- string piece
from
"_SYS_BIC"."Financial_Planning_and_Analysis/ZZTPARPAR_SOURCE" as ST, -- source table
SERIES_GENERATE_INTEGER(1, 0, 10 ) as NT -- numbers table
) tbl
where splitted is not null
order by PARAM, Element_Number;
End
不是真的。。。这是所有SQL变体中非常常见的请求,答案很简单。由于HANA SQL没有内置函数,因此需要用户编写自己的函数。因为我知道人们最终会搜索StackOverflow这个函数,所以我认为提出这个问题会很好。然后在你的厌食症问题中添加一些这条评论的内容。如果我问的是同样的问题,但说的是C而不是HANA,每个人都会直觉地知道我在问什么。我不认为这个问题有什么问题。Thx为您的可用代码:如何在另一个函数中使用输出?还是没有更简单的方法?
Begin
VAR_OUT =
select *
from ( select
ST.PARAM, -- Source table id
NT.Element_Number, -- Occurrence number within source table row
SUBSTR_REGEXPR( '([;])([^;]*)(?=;)'
IN CONCAT(CONCAT(';',ST.VALUE),';')
OCCURRENCE NT.Element_Number
GROUP 2
) splitted -- string piece
from
"_SYS_BIC"."Financial_Planning_and_Analysis/ZZTPARPAR_SOURCE" as ST, -- source table
SERIES_GENERATE_INTEGER(1, 0, 10 ) as NT -- numbers table
) tbl
where splitted is not null
order by PARAM, Element_Number;
End