Skip to main content

SUBSTR and INSTR Character Function in Oracle #DOTC_MDB

 

Character Function SUBSTR and INSTR in Oracle

 

Hello Everyone ,

Today we will discuss about character Function SUBSTR and INSTR in Oracle SQL and its uses.

In SQL Character Functions mainly operate on Character data , Character Function can be used with columns having character data.

 SUBSTR :  SUBSTR functions allows you to extract a substring from a string. SUBSTR function returns a string value . If length is negative then SUBSTR function return a null value.

Note:  If the position is positive, then Oracle Database counts from the beginning of char to find the first character. If the position is negative, then Oracle counts backward from the end of char

Example of SUBSTR Function in Oracle SQL.

To show uses of SUBSTR with different parameter I will take example from dual table and existing table in a database.

To get the first 3 character we need to write below SQL query by using SUBSTR function to get the result.

// GET FIRST 3 CHARACTER FROM DUAL TABLE

 SELECT SUBSTR('BANGALORE', 1, 3) AS LOCATION FROM dual;

Below is the output of above query .



// TO GET LAST 3 CHARACTER (NEGATIVE START POSITION)

 SELECT SUBSTR('BANGALORE', -3) FROM dual;

Below is the output of above Query .

 


// GET THE VALUE FROM 4TH BELOW IS THE QUERY

 SELECT SUBSTR('BANGALORE', 4) AS LOCATION FROM dual;

 

BELOW IS THE OUTPUT OF ABOVE QUERY.



 

IN SAME MANNER WE CAN USE SUBSTR WITH NEGATIVE AND POSITIVE NUMBER TO SEE THE RESULT AS PER OUR REQUIREMENT.

 SELECT SUBSTR('BANGALORE', -5,3) AS LOCATION FROM dual;

Above query will return ALO

SELECT SUBSTR('BANGALORE', -8,2) AS LOCATION FROM dual;

Above query will return AN .

You can check SUBSTR in a table and for particular column you can get the result in same manner.

//Using SUBSTR in a base table

 SELECT SUBSTR(name, -5,2) from employee where emp_id in(9876,2000);

Below is the output of above query .

 


//Using SUBSTR to see the last character we need to use below query

 

SELECT SUBSTR(name, -1) from employee where emp_id in(9876,2000);

Output of above query is as below:

 


Try to use SUBSTR as per your data in a table.

 

INSTR :  INSTR function In SQL  returns the location of a substring in a string. INSTR function returns a numeric value. The first position in the string is 1.If substring is not found in string, then the INSTR function will return 0.

Note : INSTR function searches for a substring in a string and returns the position of the substring in a string.

Example of INSTR Function in SQL.

// USING INSTR TO SEE THE FIRST OCCURRENCE OF A BY USING BELOW QUERY

SELECT INSTR('MY NAME IS ABCDE', 'A') FROM DUAL;

SELECT INSTR('MY NAME IS ABCDE', 'A', 1, 1) FROM DUAL;

BOTH QUERIES WILL GIVE THE POSITION OF THE FIRST OCCURENCE OF CHARACTER A IN STRING AS NUMBER.

Output of above query is as below.

 


// Second occurrence of A by using below query

SELECT INSTR('MY NAME IS ABCDE', 'A', 1, 2) from dual

Output of above query is as below

 


 

// Third occurrence of A by using below query

SELECT INSTR('MY NAME IS ABCDE AND ', 'A', 1, 3) FROM DUAL

  Output of above query is as below:

 


// SECOND OCCURENCE FROM BACK COUNTING AND IT WILL GIVE RESULT AS 12

 

SELECT INSTR('MY NAME IS ABCDE AND ', 'A', -3, 2) FROM DUAL

Output of above query is as below:


//Using INSTR  Function in a base table

SELECT ENAME, INSTR(ENAME,'A') as NAME_POS

FROM EMP

Below is the output of above query.

 


Please go through this blog and practice Character Function SUBSTR and INSTR as per your requirement and for any questions please let me know.  In next blog I will come up with more character functions like TRIM,Length,LPAD,RPAD,TRIM etc.

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Concept of Software Development life Cycle in IT industry #DOTC_Mdb

Concept of Software Development life Cycle in IT industry.   Hello Everyone, In my previous blog we discussed about Agile Model which is also a part of SDLC, but today we discussed about Software Development life cycle in details and how being software products developed or services provided through SDLC. I suggest you to please go through my previous blog that is related to Agile model also a part of SDLC which permits a company to develop software in small, quick segments. https://dheeraj60.blogspot.com/2020/07/overview-of-agile-modelmethodology.html Let’s discuss about SDLC as its play very important role in developing software products as well as providing services to the clients in simple word we can say SDLC(Software Development life Cycle ) is a process that produces software with the highest quality and lowest cost in the shortest time. SDLC includes a detailed plan for how to develop, alter, maintain, and replace a software system .SDLC is also known as Software devel...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...