-2

In SQL HANA, I need to find how many times a given word is repeated in a string column whose values are delimited by "," and output it as a separate column.

Example, the string column contains:

ZN,ZN,ZS,ZQ

Expected result for "ZN":

2
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
john
  • 125
  • 1
  • 4
  • 11

3 Answers3

1

You might find it acceptable to search only the string ZN by ignoring the fact that there's a comma.

You may count the number of occurrences of any substring by using the string function OCCURRENCES_REGEXPR:

SELECT OCCURRENCES_REGEXPR('(ZN)' IN STRINGCOLUMN) "occurrences_zn" FROM TABLE;

If you really want to clearly specify that ZN is to be searched as an entire word between commas or at the edges, then you may find a better regular expression (the question is then more about regular expressions and not SQL HANA, and you may find existing answers in Stack Overflow).

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
0

I can't remember where I found the trick, but in SQL Server, the following works like a charm:

DECLARE @myStringToSearch nvarchar(250) = 'ZN,ZN,ZS,ZQ'
DECLARE @searchValue nvarchar(5) = 'ZN'

SELECT (LEN(@myStringToSearch) - LEN(REPLACE(@myStringToSearch, @searchValue, ''))) / LEN(@searchValue)

The last line compares the length of the original string with the length of the same string, but this time replacing your search value (ZN) with a blank string. In our case, this would result in 4, because ZN is 2 characters, and it was removed twice. However, we're not interested in how many characters were removed, but in how many times the value was encountered, so we divide that result by the length of your search string (2).

Output of the query:

2

You could easily implement this as a DEFAULT constraint in your table, provided your search string is the same across every row.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
-1

I wrote one anonymous block in sql , which can be converted to HANA Table function and can be used to achieve expected result.

DO

BEGIN

DECLARE FULL_STRING VARCHAR(100);

DECLARE TRIM_STRING VARCHAR(100);

DECLARE VAL_STRING VARCHAR(100);

FULL_STRING ='ZN,ZN,ZS,ZQ';

FULL_STRING=CONCAT(FULL_STRING,',');

--SELECT :FULL_STRING FROM DUMMY;

VAL_STRING=SUBSTRING(:FULL_STRING,1,LOCATE(:FULL_STRING,',',1)-1);

VAR_TABLE=SELECT :VAL_STRING STRINGVAL FROM DUMMY;

TRIM_STRING=SUBSTRING(:FULL_STRING,LOCATE(:FULL_STRING,',',1)+1 ,LENGTH(:FULL_STRING));

--SELECT * FROM :VAR_TABLE;

--SELECT :TRIM_STRING FROM DUMMY;

WHILE :TRIM_STRING IS NOT NULL AND LENGTH(:TRIM_STRING)>0

DO VAL_STRING=SUBSTRING(:TRIM_STRING,1,LOCATE(:TRIM_STRING,',',1)-1);

--SELECT :VAL_STRING FROM DUMMY;

VAR_TABLE=SELECT STRINGVAL FROM :VAR_TABLE UNION ALL SELECT :VAL_STRING FROM DUMMY;

TRIM_STRING=SUBSTRING(:TRIM_STRING,LOCATE(:TRIM_STRING,',',1)+1 ,LENGTH(:TRIM_STRING)); --i=i+1;

--SELECT :TRIM_STRING FROM DUMMY;

END WHILE ;

SELECT STRINGVAL,COUNT(STRINGVAL) FROM :VAR_TABLE GROUP BY STRINGVAL;

--SELECT :TRIM_STRING FROM DUMMY;

Aniruddha Shinde
  • 124
  • 1
  • 2
  • 11