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.
It is very useful for me. Thanks...
ReplyDeleteDevOps Online Training
Best DevOps Online Training in Hyderabad
Nice Blog and its very useful for me. Thanks...
ReplyDeleteSnowflake Online Training