0% found this document useful (0 votes)
29 views4 pages

Character Manipulation

The document describes various Oracle SQL functions for character manipulation. Some key functions are: - CONCAT concatenates two or more strings together. - LENGTH returns the length of a string. - REVERSE returns a string in reverse order. - REPLACE finds and replaces a substring within a string. - TRIM removes leading and trailing characters like spaces from a string. - SUBSTR extracts a substring from within a string. - INSTR finds the position of a substring within a string. These functions allow for manipulating strings, extracting parts of strings, finding/replacing substrings, and other common string operations in Oracle SQL.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Character Manipulation

The document describes various Oracle SQL functions for character manipulation. Some key functions are: - CONCAT concatenates two or more strings together. - LENGTH returns the length of a string. - REVERSE returns a string in reverse order. - REPLACE finds and replaces a substring within a string. - TRIM removes leading and trailing characters like spaces from a string. - SUBSTR extracts a substring from within a string. - INSTR finds the position of a substring within a string. These functions allow for manipulating strings, extracting parts of strings, finding/replacing substrings, and other common string operations in Oracle SQL.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CHARACTER MANIPULATION FUNCTION:

CONCAT
LENGTH
REVERSE
REPLACE
TRANSLATE
SUBSTR
INSTR
TRIM
RTRIM
LTRIM
LPAD
RPAD
ASCII
CHR

CONCAT
CONCAT:
(i) It accept exactly two arguments.
(ii)It used to concatenating two input strings.

SYNTAX:
CONCAT(input_string1,input_string2)
select CONCAT('Oracle','Database') from dual;

suppose I want three strings put together.


select CONCAT(CONCAT('Oracle','Database'),'Developer') from dual;

instead of CONCAT use pipe symbol ||

select 'Oracle'||'Database'||'Developer' from dual;


select employee_id, first_name,CONCAT(first_name,employee_id) from employees;
select concat(1,2) from dual;

LENGTH:
(i) It accept only one argument.
(ii)It Display length of the string

SYNTAX:

Length(input_string1)

select LENGTH('oracle') from dual;


select LENGTH(5467) from dual;

select first_name, LENGTH(first_name) from employees;

REVERSE:
(i) It accept only one argument.
(ii)It Display string in reverse order.

select REVERSE('ORACLE') from dual;


select first_name, REVERSE(first_name) from employees;

REPLACE:
(i) It accept two or three arguments.
(ii)It used to perform find and replace.

SYNTAX:
REPLACE (input_string,finding_string,replacing_string)

select 'ORACLE DATABASE', REPLACE('ORACLE DATABASE','ab','xy') from dual; --case


sensitive
select 'ORACLE DATABASE', REPLACE('ORACLE DATABASE','AB','XY') from dual; --AB
is replaced by XY
select 'ORACLE DATABASE', REPLACE('ORACLE DATABASE','AB') from dual;
-- two arg AB is replaced with null
select LENGTH('ORACLE DATABASE'),LENGTH(REPLACE('ORACLE DATABASE','AB')) from dual;
'ORACLE DATABASE'
select REPLACE('ORACLE DATABASE','A') from dual;
select LENGTH('ORACLE DATABASE')-LENGTH(REPLACE('ORACLE DATABASE','A')) from dual;

TRANSLATE:

(i) It accept exactly three arguments.


(ii)The first character of the finding string will be replaced by first
character of the replacing string.

SYNTAX
TRANSLATE(input_string,finding_character,replacing_character)

select 'ORACLE DATABASE', TRANSLATE('ORACLE DATABASE','AB','XY')from dual;


select 'ORACLE DATABASE', TRANSLATE('ORACLE DATABASE','AB','m') from dual;
select 'ORACLE DATABASE', TRANSLATE('ORACLE DATABASE','A') from dual; --
invalid number of arguments

DIff:

Replace: used to replace the characters by another set of characters.


Translate: is used for character by character substitution or replacements.

SUBSTR:

(i) It accept two or three arguments.


(ii) It used to get substring from input string.

SYNTAX
SUBSTR(input_string,starting_position,length)

select 'ORACLE DATABASE' ,SUBSTR('ORACLE DATABASE',4,6)from dual; --3 arg


select 'ORACLE DATABASE' ,SUBSTR('ORACLE DATABASE',4) from dual; --2 arg

select 'ORACLE DATABASE' ,SUBSTR('ORACLE DATABASE',-4) from dual;


select 'ORACLE DATABASE' ,SUBSTR('ORACLE DATABASE',-4,2) from dual;
select 'ORACLE DATABASE' ,SUBSTR('ORACLE DATABASE',-2,-3) from dual; --null

Question:
empname(3 letter)+emp_id
select employee_id,first_name, CONCAT(SUBSTR(first_name,1,3),employee_id) from
employees;

INSTR:
(i) It accept two or four arguments.
(ii) It used to find the position of finding string.
SYNTAX:
INSTR(input_string,finding_string,starting_position,nth occurance)

select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',1,1) from dual;


select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',1,2) from dual;
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',4,3) from dual;
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',4,5) from dual;
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A') from dual;
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',1,1) from dual;
-- so default it taken as 1,1
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE','A',1) from dual;
select 'ORACLE DATABASE', INSTR('ORACLE DATABASE') from dual; -- not enough
arguments for function

/*Extract name and domain from email*/

select email,SUBSTR(email,1,INSTR(email,'@',1,1)-1)Name,
SUBSTR(email,INSTR(email,'@',1,1)+1) Domain_name from t1;

TRIM:
(i). It accept one argument
(ii).It is used to remove special characters or spaces on both sides.

SYNTAX:
TRIM(special_character from input_string)

select TRIM('*' from '***ORACLE***') from dual;


select '***ORACLE**DB***',TRIM('*' from '***ORACLE**DB***') from dual;
select LENGTH(' ORACLE DB '),LENGTH(TRIM(' ORACLE DB '))from
dual;
select first_name,last_name from employees where first_name=TRIM(' Steven ');

RTRIM:
(i). It accept one or two Arguments
(ii).It is used to remove special characters or spaces on right hand side.

RTRIM (input_string,special_char)

select RTRIM('*****ORACLE*****','*')from dual;


select ' ORACLE ', RTRIM(' ORACLE ')from dual;
select LENGTH(' ORACLE '), LENGTH(RTRIM(' ORACLE '))from dual;
select first_name,last_name from employees where first_name=RTRIM('Steven ');

RPAD:
It accept two or three Arguments
It is used to adding special characters on RIGHT hand sides

SYNTAX:
RPAD(input_string,length,padding_character)

select RPAD('ORACLE' ,10,'*') from dual; --3 arg


select RPAD('ORACLE',15) from dual; --2 arg
select LENGTH('ORACLE'),LENGTH(RPAD('ORACLE',15)) from dual;

select first_name,RPAD(first_name,10) from employees;


select first_name,RPAD(first_name,10,'*') from employees;
ASCII
American Standard Code for Information Interchange

(i) It accept one argument.


(ii)It is used to get ASCII value for given input character.

Syntax(input_character)

select ASCII('A') from dual;


select ASCII('a') from dual;

CHR:
(ii).It accept one argument.
(ii).It used to get character value from ASCII char.

select CHR(42) from dual;


select CHR(97) from dual;

You might also like