Oracle INSTR function

Oracle INSTR function returns the position of the sub string string within a string. Function returns an integer value. It is an example of character functions.

Syntax:

INSTR(string, substring,[position],[place])

position and place are optional.

INSTR can be used in PL/SQL too.

Examples:-

1.select instr(‘sqlandplsql.com’,’.com’) as instrvalue from dual;

INSTRVALUE
——————–
12

Returns the position of sub string  ‘.com’ .

2. select instr(‘sqlandplsql.com’,’sql’,1,1) as instrvalue from dual;

INSTRVALUE
——————–
1

Returns the first occurrence of ‘sql’

3. select instr(‘sqlandplsql.com’,’sql’,1,2) as instrvalue from dual;

INSTRVALUE
——————–
9

Returns the second occurrence of ‘sql’

4. Use INSTR for string manipuation

Consider the table website with below records

No    Url

—       ——————————

1       http://www.yahoo.com
2       http://www.google.com
3       http://www.bbc.com
4       http://www.cnn.com
5       http://www.amazon.com
6       http://www.wikipedia.com

Below sql will return the website url text only ( Using both SUBSTR and INSTR functions)

select substr(url,instr(url,’.’,1,1)+1,instr(url,’.’,1,2) – (instr(url,’.’,1,1)+1) )  as urltext from website;

URLTEXT
——————–
yahoo
google
bbc
cnn
amazon
wikipedia

Advertisements

Author: sqlandplsql

To learn more about the Oracle, SQL, PL SQL, Performance Tuning, Database Modeling, Ubuntu, MySQL etc .Suggestions, comments, feedbacks and referrals are highly appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s