How to Use character Function in Oracle
Character functions
This functions returns the string in form_str, replaced by the corresponding character in to_str.
Thus, we learnt, how to use character functions in Oracle with the examples.
- CHR(x)
- CONCAT(string1, string2)
- INITCAP(string)
- LOWER(string)
- UPPER(string)
- LPAD(char1, n, [char2])
- RPAD(char1, n, [char2])
- LTRIM(string, ‘char/s’)
- RTRIM(string, ’char/s’)
- REPLACE(string, search_str, [replace_str])
- SUBSTR(string, m, [n])
- TRANSLATE(string, from_str, to_str)
- CHARACTER FUNCTIONS Returning numeric values
- ASCII(string)
- INSTR(STRING, CHAR)
- LENGTH(STRING)
CHR(x)
This function returns the character, which has the value equivalent to x in the database character set. CHR and ASCII are the opposite functions.
Example
- Select CHR (35) a, CHR (102) b, CHR (104) c
- from dual;
CONCAT(string1, string2)
This function returns string1 concatenated with string2. This function is identical to the || operator.
Example
- select CONCAT('Hello', 'good morning') "wishes"
- from dual;
INITCAP(string)
This function capitalizes the first character of each word in the string.
Example
- select INITCAP(Emp_name)
- from Employee;
LOWER(string)
This function converts the string to lower case.
Example
- Select LOWER(Emp_name), LOWER(‘xyz’)
- from Employee;
UPPER(string)
This function converts the string into upper case.
Example
- Select UPPER(Emp_name), UPPER('xyz')
- From Employee;
LPAD(char1, n, [char2])
This function pads the column or literal value from the left side, to a total width of n character positions. The leading spaces are filled with char2.
Example
- select LPAD(Emp_name, 10, '#'), LPAD(Emp_name, 10, ' ')
- from Employee;
RPAD(char1, n, [char2])
This function pads the column or literal value from the right side, to a total width of n character positions. The trailing spaces are filled with char2.
Example
- Select RPAD(Emp_name, 10, ‘*’), RPAD(Emp_name, 10, ‘ ’)
- from Employee;
LTRIM(string, ‘char/s’)
This functions removes all the blank spaces from the left.
Example
- Select Emp_name, LTRIM(Emp_name), LTRIM(Emp_name, ‘R’)
- from employee;
RTRIM(string, ‘char/s’)
This functions removes all the blank spaces.
Example
- Select Emp_name, RTRIM(Emp_name), RTRIM(Emp_name, ‘R’)
- from Employee;
REPLACE(string, search_str, [replace_str])
This function returns the string with every occurrence of search_str replaced with replace_str.
Example
- Select REPLACE ('Ball and Battle', 'B', 'c' ) "First"
- from Dual;
- Select REPLACE ('Ball and Battle', 'B' ) "Second"
- from Dual;
- Select REPLACE ('Ball and Battle', NULL ) "Thrid"
- from Dual;
TRANSLATE(string, from_str, to_str)
Translate is a superset of the functionality, provided by replace.
Example
- select TRANSLATE ('qwertyuiop', 'uiop', '1234')
- from dual;
Character Function Returning Numeric Values
ASCII(string)
This function returns the decimal of the first byte of the string in the character set.
Example
- select ASCII(' ')
- from Dual;
- select ASCII('x')
- from Dual;
INSTR(string, char)
This function returns the position of the first occurrence of char in the string.
Example
- select Emp_name, INSTR(Emp_name, 'a')
- from Employee;
LENGTH(string)
This functions returns the length of a string. This function applies, only with varchar2.
Example
- Select Emp_name, LENGTH(Emp_name)
- From Employee;
Summary