Character Fu Ctio S: Char, Varchar2, Varchar Etc
Character Fu Ctio S: Char, Varchar2, Varchar Etc
CTIO
S
1
Single-row Character Functions
2
Examples
SELECT ASCII('Ç')
FROM DUAL;
ASCII ( 'Ç' )
199
3
SELECT INSTR('MISSISSIPPI' , 'S' ,5 , 2)
FROM DUAL;
INSTR('MISSISSIPPI','S',5,2)
7
RPAD(E
AME,20,'*')
SMITH***************
ALLEN***************
14 rows selected.
4
TRIM
TRIM('TEKNIK')
teknik
TRIM(''FROM'TEKNIK
teknik
TRIM(LEAD
123
5
SELECT trim(trailing '1' from 'Tec1h111')
FROM dual
TRIM(TRAILING'1
Tec1h
TRIM(BOTH'1'FROM'1
23Tech
6
SOUNDEX
SELECT ENAME
FROM EMP
WHERE SOUNDEX(ename) = SOUNDEX('SMYTHE');
E
AME
SMITH
Character functions:
A. Case Conversion Functions
• LOWER
• UPPER
• INITCAP
• SUBSTR
• LENGTH
• INSTR
• LPAD
• RPAD
• TRIM
7
• LTRIM
• RTRIM
• ASCII
• CHR
• REPLACE
• TRANSLATE
8
Case Conversion Functions
Function Result
9
Using Case Conversion Functions
Display the employee number, name, and department number for
employee Blake.
SELECT empno, ename, deptno
FROM emp
WHERE ename = 'blake';
no rows selected
The WHERE clause of the first SQL statement specifies the employee name
as ' blake.' Since all the data in the EMP tabls is stored in uppercase. the
name ' blake' does not ffind a match in the EMP table and as a result no rows
are selected.
..........................................................
EMP
O E
AME DEPT
O
7698 BLAKE 30
The WHERE clause of the second SQL statement specifies that the employee
name in the EMP table be converted to lowercase and then be compared to
'blake ' . Since both the names are in lowercase now, a match is found and
one row is selected. The WHERE clause can be rewritten in the following
manner to produce the same result:
10
Using Character Manipulation Functions
ote: RPAD character manipulation function pads the character value lelt-
justified.
11
Using the Character Manipulation
Functions (continued)
Example:
The slide example displays ernployee name and job joined together,
length of the employee name, and the numeric position of the letter A in
the employee name, forall employees who are in sales.
E
AME CO
CAT(E
AME,JOB) LE
GTH(E
AME) I
STR(E
AME,'A')
ALLEN ALLENSALESMAN 5 1
WARD WARDSALESMAN 4 2
MARTIN MARTINSALESMAN 6 2
TURNER TURNERSALESMAN 6 0
12
Using the Character Manipulation
Functions (continued)
Example
Write a SQL statement to display the data for those employees whose
names end with an N .
E
AME CO
CAT(E
AME,JOB) LE
GTH(E
AME) I
STR(E
AME,'A')
ALLEN ALLENSALESMAN 5 1
MARTIN MARTINSALESMAN 6 2
13