Let's extract STRING!





SUBSTR

Syntax: SUBSTR ( source string, start position [, number of characters to extract])

Parameters: source string is the string from which sub-string is to be extracted. Start position is the starting position of extraction. Number of characters to extract is the optional parameter. If this parameter is omitted, the SUBSTR function will return the entire string.

Return type: The SUBSTR function returns a string value. If number of characters to extract is a negative number, then SUBSTR function will return a NULL value.

Usage: Manager got an offer from mobile phone carrier provider for all the employees of the organization. To avail this offer, Manager has to group the employees based on the area code in their phone number. 
Manager to Data analyst: Bring me the list of all employees with first three digits of their phone no. sort the result based on phone no. Data analyst runs below query to fetch the required data.


SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SUBSTR(PHONE_NUMBER,1,3) AS AREA_CODE FROM EMPLOYEES ORDER BY PHONE_NUMBER;

Comments

Popular posts from this blog

Let's learn Left Outer Join!

Let's learn Cross Join!