SUBSTR oracle code example

Example 1: oracle substring

-- ORACLE substr(string, start, [, length ])
SELECT substr('Hello World', 4, 5) FROM DUAL;       -- lo Wo
SELECT substr('Hello World', 4) FROM DUAL;          -- lo World
SELECT substr('Hello World', -3) FROM DUAL;         -- rld

Example 2: plsql left() function

SUBSTR( "20190601", 0, 6 )

Example 3: plsql substr

SUBSTR( string, start_position [, length ] )

Example 4: oracle leftmost characters

-- For Oracle only

-- syntax 
SUBSTR(<main-string>,1,<number-of-characters>)

-- example 
SUBSTR('Useless stuff',1,10) -- OUTPUT: Useless st

-- practical example
SELECT SUBSTR('Useless stuff',1,10)
FROM DUAL;

Example 5: ORACLE SQL SUBSTR

SUBSTR(string, :START_POS, :SUBSTR_LENGTH);

SELECT SUBSTR('ABCDEFG',3,4) FROM DUAL;
--OUTPUT: CDEF

Example 6: Subtr Oracle ?

/*Using SUBSTR in Oracle (Example from hackerrank.com): */

/*Simple select query...*/ 
SELECT DISTINCT city 
FROM station 

/*Using WHERE and SUBSTR to find (distinct) cities in station table that begin 
as well as end with a vowel.*/ 
WHERE SUBSTR(city,1,1) IN ('A','E','I','O','U') 
	AND substr(city,-1) IN ('a','e','i','o','u'); 

/*Parameters for SUBSTR (Substring) in order are as follows: 
String, Start, Length.*/

Tags:

Sql Example