Let's review INSTR!!







INSTR

Syntax: INSTR ( source string, search string [, start position, nth occurance])

Parameters: source string is the string in which search string is to be searched. Start position is the starting position for searching. It is optional. If it is omitted, its default value is 1. The first position in the source string is 1. If the start position is negative, The INSTR function counts back start position number of characters from the end of string and the searches towards the beginning of string. Nth occurrence is the occurrence of substring which we want. It is an optional parameter. If it is omitted, its default value is 1.

Return type: The INSTR function returns a numeric value. If the search string is not found in source string, INSTR function returns 0.

Usage: Manager to data analyst: Bring me the second word in the street address of each employee. Data analyst runs below query to fetch the required data.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SUBSTR(STREET_ADDRESS, INSTR(STREET_ADDRESS,' ',1,1)+1, INSTR(STREET_ADDRESS,' ',1,2)-INSTR(STREET_ADDRESS,' ',1,1)-1) 
FROM LOCATIONS JOIN DEPARTMENTS USING(LOCATION_ID)
JOIN EMPLOYEES USING(DEPARTMENT_ID);



Comments

Popular posts from this blog

Let's learn Cross Join!

Full Outer Join!

Let's extract STRING!