How to get a count of number of occurrence of a substring in a string in teradata? - teradata

I have a column in a teradata table with string values like "page1-->page2-->page1-->page3-->page1--page2-->..."
I want to search for a specific page and get the number of occurrence of the page in the string. I couldn't find any function that gives this result.

There's no builtin function, but there's a common solution:
Remove all occurences of the substring from the string and compare the length before/after:
(Char_Length(string) - Char_Length(OReplace(string, searchstr))) / Char_Length(searchstr)
Edit:
For a wildcard search you can utilize REGEXP_REPLACE:
Char_Length(RegExp_Replace(RegExp_Replace(s, 'page1(.+?)page3', '#',1,0), '[^#]','',1,0))
For `#' use a character which is known not to be in your input string.

Related

get only numbers inside parenthesis filter or custom filter or what?

The string is "Some Words(1440)" and I want to store the numbers inside the parenthesis as a variable in twig so it can be output and used. I thought maybe I could do it with a split but I wasn't able to escape the parenthesis properly.
What I have:
Some Words (1440)
What I want to extract from the string is just the numbers in parenthesis
1440

Use regex to lookup and extract a piece of string

I’ve been looking around and haven’t found a clear answer to the following:
In R, what regex-function and what regex-string should I use to lookup an specific pattern and extract a specific part of that pattern?
For example:
Input string:
"aaabbs11:00.4.3(1111S)cccsdd(3332d)"
Desired output: the part within the brackets after the 11:00.4.3, so
# 1111S
If you want the first string in parenthesis after \d\d:\d\d.\d.\d, then
.*?\d{2}:\d{2}\.\d\.\d.*?\((.*?)\)
If the number of digits can vary, replace {2} with *, and add * after the \d without any quantifiers.

Is it possible to select rows if a string column contains any number value?

A particular column in my table is marked as a String type, however, more than likely, it will have an integer value mixed in with the string.
Here are some example rows:
"hello1"
"keys"
"Thom27"
"3for5"
I would like to be able to select the rows that have numbers in the string. Thank you.
You can use GLOB for that:
SELECT *
FROM tblNames
WHERE Name GLOB '*[0-9]*'
GLOB is the best answer (as mentioned above). GLOB supports REGEX like matching, which is more efficient than individual matches with LIKE.

String Aggregation in sqlite

Anyone knows if String Aggregation in sqlite is possible?
If i have an animal column with 5 rows/datas, how can i combine them so that the output would be in one field
'dog','cat','rat','mice','mouse' as animals
Thanks
You're looking for something like the following:
select group_concat(animal) from animals;
This will return something like the following:
dog,cat,rat,mice,mouse
If you don't want to use a comma as the separator, you can add your own separator as a second parameter:
select group_concat(animal, '_') from animals;
which will return:
dog_cat_rat_mice_mouse
I think this will be useful:
group_concat(X)
group_concat(X,Y)
The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.

SQLite query - using [] in SQLite queries

I would like to know if it is possible to use [] in SQLite query as we used to in Access and other DB.
e.g. SELECT * FROM mytable WHERE fwords like '%b[e,i,a]d%'
this will retrieve all rows have fwords containing bad, bed, bid
Thanks a lot
From http://www.sqlite.org/lang_expr.html:
The LIKE operator does a pattern matching comparison. The operand to the right of the LIKE operator contains the pattern and the left hand operand contains the string to match against the pattern. A percent symbol ("%") in the LIKE pattern matches any sequence of zero or more characters in the string. An underscore ("_") in the LIKE pattern matches any single character in the string. Any other character matches itself or its lower/upper case equivalent (i.e. case-insensitive matching).
Does that help?
You can have a look at the regex section here.

Resources