INSTR function in Teradata

By | 6th October 2021

Teradata has multiple functions to work with strings. INSTR function is a very handy one when it comes to search a string inside another. In the other blog, we saw how this is done using the INDEX function. In this, we will see the usage of the INSTR function.

Unlike the INDEX function, the INSTR function has much more ability to specify the position from where the search can be started. Another feature of this function is that it also gives us the flexibility to find the nth occurrence of a string.

Syntax

INSTR(String expression, String to search, Starting position, Nth occurrence)

String expression – The full string value inside which the search will be done

String to search – The string to search inside the original string value

Starting position – The position from where the search can be done

Nth occurrence – The nth occurrence of the searched string

Let us see an example to understand all possible usage of the INSTR function.

Imagine you are searching for the keyword ‘ERROR’ inside a column where the application’s error message is stored. There are multiple ways in which the INSTR function can be used in this scenario.

Case 1 – With two arguments

SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR') pos;

In the above example, we are only using two arguments. The result will give the position of the word ‘ERROR’. This case is the same as the INDEX function. The result will give the position of the first occurrence of the word ‘ERROR’

Output – Case 1

Case 2 – With three arguments

SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 3) pos;

In the above query, you can see that we have used three arguments. The third argument will tell the query to start the search from the mentioned position. If you notice, there are two places where the word ‘ERROR’ is present in the string. One is at the first position and the other is at the 38th position. Since we have given the starting position of the search as 3, it will ignore the first occurrence of the word ‘ERROR’ which is at the first position.

Output – Case 2

The same query with position value as 1 will return the output as 1 as we have the word ‘ERROR’ present at the first position.

SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1) pos;

Case 3 – With four arguments

SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1, 2) pos;

In this query, we have included all the possible number of arguments supported by the INSTR function. The fourth argument here represents the Nth occurrence of the word that we are searching for.

The 4th argument is given as 2. This means, that we are searching for the second occurrence of the word ‘ERROR’ starting from the first position.

Output – Case 3

In the same example, if you change the fourth argument value to 1, you will get the output position as 1 as we have the first occurrence of the word ‘ERROR’ in the first position itself

SELECT INSTR('ERROR: Your query failed with SYNTAX ERROR ','ERROR', 1, 1) pos;

Also read,

  1. INDEX function in Teradata
  2. Concatenation operation in Teradata
  3. Substring function in Teradata
  4. Fetching Current Date and Time in Teradata