Saturday, November 29, 2008

Oracle - Instr function

Instr function in Oracle

Instr retruns the position or Index of a character or a sub string in a string.

It is very use full and eases to use for many kind of searching a string in a string at specific positions and from specific location of a string.
Syntax: - Instr(String1,Sub String 1,[Position or Index to start search],[Position (nth) occurance].

[Position or Index to start search] --- Indicates the position from where to start search in given string for sub string. If the value is negative, it means to perform search in string from back to front for the sub string. Where as if positive it performs search in string from front to back. By default the value is 1. This is optional.

[Position (nth) occurrence] --- Indicates to consider the character occurrence or appearance count while performing search. By default the value is 1. This is optional.













Example:-
Input Query Output Result

Instr(‘abcdefghabcdefgh’,’abcdefgh’)
(Starts search from abcdefghabcdefgh and first occurrence of ‘a’ from a is at 1 st position)
1

Instr(‘srinivas’,’s’)
(Starts search from srinivas and first occurrence of‘s’ from s is at 1 st position)
1


Instr(‘srinivas’,’s’,2,1)
(Starts search from srinivas and first occurrence of‘s’ from r is at 8 th position)
8

Instr(‘srinivas’,’s’,-2,1)
(Searches back to front)
(Starts search from srinivas and first occurrence of‘s’ from a is at 1 st position)
1