Oracle SQL String Manipulation Functions Guide
180 likes | 318 Vues
Learn to manipulate characters in Oracle SQL with single row functions like CONCAT, RPAD, LPAD, TRIM, UPPER, LOWER, INITCAP, LENGTH, SUBSTR, INSTR. Examples and tips included.
Oracle SQL String Manipulation Functions Guide
E N D
Presentation Transcript
SINGLE ROW FUNCTIONS 1. CHARACTER MANIPULATION Prof. Carmen Popescu Oracle Academy Lead Adjunct
Concatenation • str1 || str2 • CONCAT(str1,str2) • To concatenate more that 2 strings: • Str1 || str2 || str4 • CONCAT(str1,CONCAT(str2,str3))
RPAD, LPAD • RPAD(string,length,['set']) • LPAD(string,length,['set']) If 'set' does not appear the string will be padded with spaces.
RPAD, LPAD Examples SELECT RPAD('ab',10,'<>') FROM DUAL => ab<><><><> SELECT LPAD('xyz',15,'*') FROM DUAL • ************xyz
RPAD, LPAD Examples SELECT LPAD(RPAD('abc',8,'='),13,'=') FROM DUAL => =====abc===== SELECT LPAD(RPAD('abc',8,'='),13,'=') FROM DUAL => =====abc===== SELECT LPAD(RPAD('abc',8,'='),13,'=') FROM DUAL => =====abc=====
LTRIM, RTRIM • Trim off unwanted characters from the left (LTRIM), right (RTRIM) • LTRIM(string,[’set’]) • LTRIM(string,[’set’]) • Set is the collection of characters you want to trim off. If no set is specified, the function trim off spaces.
LTRIM, RTRIM Examples SELECT LTRIM('**==**abc**==**','*') FROM DUAL => ==**abc**==** SELECT RTRIM('**==**abc**==**','*') FROM DUAL => **==**abc**==
TRIM • trim ( [leading|trailing|both ['set'] FROM] s1) • Trim off set from the beginning, the end or both of the string s1 • both option is Implicit • If no set is specified, the function trim off spaces.
TRIM Examples SELECT TRIM(leading 'ab' from 'ababbabbabab') FROM DUAL => babababab
TRIM Examples SELECT TRIM(trailing 'ab' from 'ababbabbabab') FROM DUAL => ababbabb
TRIM Examples SELECT TRIM('ab' from 'ababbabbabab') FROM DUAL => babb
LOWER, UPPER, INITCAP • UPPER(string) • LOWER(string) • INITCAP(string)
LOWER, UPPER, INITCAP SELECT UPPER('aBcD'), LOWER('aBcD'), INITCAP('aBcD') FROM DUAL SELECT UPPER('aBcD'), LOWER('aBcD'), INITCAP('aBcD') FROM DUAL => ABCD SELECT UPPER('aBcD'), LOWER('aBcD'), INITCAP('aBcD') FROM DUAL => ABCD abcd SELECT UPPER('aBcD'), LOWER('aBcD'), INITCAP('aBcD') FROM DUAL => ABCD abcd Abcd
LENGTH SELECT LENGTH('abc') FROM DUAL => 3
SUBSTR SUBSTR(string,start[,count]) - If no count is specified, the function return the substring starting at position start and going to the end of the string. SELECT SUBSTR('abcdefghi',5,2) FROM DUAL • ef SELECT SUBSTR('abcdefghi',5) FROM DUAL • efghi
INSTR INSTR(string,substring[,start[,n]]) • Search for the n-th occurrence of the substring in the string, starting with the start position • If the start position is not mention the function will look for the substring starting at position 1. • If n is not mention the function will look for the first occurrence of the substring in the string
INSTR examples SELECT INSTR('xyzabcxabcxabcxyz','abc') from dual • 4 SELECT INSTR('xyzabcxabcxabcxyz','abc',6) from dual • 8 SELECT INSTR('xyzabcxabcxabcxyz','abc',1,2) from dual • 8 SELECT INSTR('xyzabcxabcxabcxyz','abc',5,2) from dual • 12
SUBSTR, INSTR Together SELECT Author, SUBSTR(Author, INSTR(Author,',')+2 ) || ' ' || SUBSTR(Author,1, INSTR(Author,',')-1 ) FROM Magazine Results: Eminescu, MihaiMihaiEminescu Creanga, IonIonCreanga Cosbuc, GeorgeGeorgeCosbuc