0% found this document useful (0 votes)
6 views

String_functions

Uploaded by

sangmeshwar8484
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

String_functions

Uploaded by

sangmeshwar8484
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

------------------------------------------String

Functions------------------------------------------------
1. UPPER():- The Oracle/PLSQL UPPER function converts all letters in the specified
string to uppercase.
If there are characters in the string that are not letters, they are
unaffected by this function.

EXAMPLE- SELECT UPPER('achiver')


FROM DUAL;
SELECT FIRST_NAME, UPPER(FIRST_NAME)
FROM EMPLOYEES;

2. LOWER():- The Oracle/PLSQL LOWER function converts all letters in the specified
string to lowercase.
If there are characters in the string that are not letters, they are
unaffected by this function.
EXAMPLE- SELECT LOWER('ACHIEVER')
FROM DUAL;
SELECT FIRST_NAME, LOWER(FIRST_NAME)
FROM EMPLOYEES;

3. LENGTH()- The LENGTH () function returns the number of characters of the given
string or word.
EXAMPLE- SELECT LENGTH('ACHIEVER')
FROM DUAL;
SELECT FIRST_NAME, LENGTH(FIRST_NAME)
FROM EMPLOYEES;
4. SUBSTR() - The Oracle/PLSQL SUBSTR functions allows you to extract a substring
from a string.

Syntax
The syntax for the SUBSTR function in Oracle/PLSQL is:

SUBSTR( string, start_position [, length ] )

Parameters or Arguments
string
The source string.
start_position
The starting position for extraction. The first position in the string is always 1.
length
Optional. It is the number of characters to extract. If this parameter is omitted,
the SUBSTR function will return the entire string.
Returns
The SUBSTR function returns a string value.
If length is a negative number, then the SUBSTR function will return a NULL value.

Note
If start_position is 0, then the SUBSTR function treats start_position as 1 (ie:
the first position in the string).
If start_position is a positive number, then the SUBSTR function starts from the
beginning of the string.
If start_position is a negative number, then the SUBSTR function starts from the
end of the string and counts backwards.

EXAMPLE- SELECT SUBSTR('This is a test', 6, 2) AS "SUBSTRING" FROM DUAL


SELECT SUBSTR('This is a test', 6) AS "SUBSTRING" FROM DUAL
SELECT SUBSTR('This is a test', -9,2) AS "SUBSTRING" FROM DUAL
SELECT SUBSTR('This is a test', -6) AS "SUBSTRING" FROM DUAL
SELECT SUBSTR('This is a test', 0, 2) AS "SUBSTRING" FROM DUAL

5. INITCAP() :- The Oracle/PLSQL INITCAP function sets the first character in each
word to uppercase and the rest to lowercase.

EXAMPLE-
SELECT INITCAP('tech on the net') FROM DUAL;

6.INSTR() :- The Oracle INSTR() function searches for a substring in a string and
returns the position of the substring in a string.
EXAMPLE- 1) Search from the start of the string
SELECT
INSTR( 'This is a playlist', 'is' ) substring_location
FROM
dual;
2)Search for the 2nd and 3nd occurrence of a substring
SELECT
INSTR( 'This is a playlist', 'is', 1, 2 ) second_occurrence,
INSTR( 'This is a playlist', 'is', 1, 3 ) third_occurrence
FROM
dual;
3) Search backward
SELECT
INSTR( 'This is a playlist', 'is',-1 ) substring_location
FROM
dual;

7. TRANSLATE()- The Oracle TRANSLATE() function returns a string with all


occurrences of each character in a string replaced by its corresponding character
in another string.
The TRANSLATE() function allows you to make several single-
character, one-to-one translations or substitutions in one operation.
EXAMPLE- SELECT TRANSLATE('1tech23', '123', '456') FROM DUAL
SELECT TRANSLATE('222tech', '2ec', '3it') FROM DUAL

8. REPLACE()- The Oracle REPLACE() function replaces all occurrences of a specified


substring in a string with another.
EXAMPLE- SELECT REPLACE('123123tech', '123') FROM DUAL
SELECT REPLACE('123tech123', '123') FROM DUAL

9. RTRIM()- The Oracle/PLSQL RTRIM function removes all specified characters from
the right-hand side of a string.

EXAMPLE- SELECT RTRIM('tech ') FROM DUAL


SELECT RTRIM('tech ', ' ')FROM DUAL
SELECT RTRIM('123000', '0')FROM DUAL
SELECT RTRIM('Tech123123', '123')FROM DUAL
SELECT RTRIM('123Tech123', '123')FROM DUAL
SELECT RTRIM('Techxyxzyyy', 'xyz')FROM DUAL
SELECT RTRIM('Tech6372', '0123456789')FROM DUAL

10. LTRIM():- The Oracle/PLSQL LTRIM function removes all specified characters from
the left-hand side of a string.
EXAMPLE- SELECT LTRIM(' tech') FROM DUAL
SELECT LTRIM(' tech', ' ')FROM DUAL
SELECT LTRIM('000123', '0')FROM DUAL
SELECT LTRIM('123123Tech', '123')FROM DUAL
SELECT LTRIM('123123Tech123', '123')FROM DUAL
SELECT LTRIM('xyxzyyyTech', 'xyz')FROM DUAL
SELECT LTRIM('6372Tech', '0123456789')FROM DUAL

11. TRIM():- The Oracle/PLSQL TRIM function removes all specified characters either
from the beginning or the end of a string.

EXAMPLE-
SELECT TRIM(' tech ') FROM DUAL
SELECT TRIM(' ' FROM ' tech ') FROM DUAL
SELECT TRIM(LEADING '0' FROM '00012') FROM DUAL
SELECT TRIM(TRAILING '1' FROM 'Tech1') FROM DUAL
SELECT TRIM(BOTH '1' FROM '123Tech111') FROM DUAL

12.LPAD() and RPAD():- LPAD and RPAD use to padding the value/character of left and
right side of the string/column.

LPAD( string1, padded_length [, pad_string] )


-Parameters
string1: string to be padded from left.

padded_length : length to be padded from left.

pad_string: it is optional. It is the string that will be padded from the left side
of string1.

EXAMPLE-
Select lpad('ORACLE', 10,'*') from dual;
SELECT FIRST_NAME, lPAD(FIRST_NAME,10,'*') FROM EMPLOYEES

Select rpad('ORACLE', 10,'*') from dual;


SELECT FIRST_NAME, rPAD(FIRST_NAME,10,'*') FROM EMPLOYEES

13. CHAR():- The OracleCHR() function converts an ASCII code, which is a numeric
value between 0 and 225, to a character.

EXAMPLE-
SELECT
first_name || ' ' || last_name ||
CHR( 9 ) || ' joined on ' || CHR( 9 ) ||
to_char(hire_date,'DD-MON-YYYY') employees_dtl
FROM
employees
ORDER BY
hire_date

14. CONCAT():-
The Oracle CONCAT() function concatenates two strings and returns the combined
string.

Syntax
The following illustrates the syntax of the CONCAT() function:

CONCAT(string1,string2)

Noted that the Oracle CONCAT() function concatenates two strings only.
If you want to concatenate more than two strings, you need to apply the CONCAT()
function multiple times or use the concatenation operator (||).
EXAMPLE-
SELECT CONCAT('Happy',' coding')FROM dual;

SELECT CONCAT( CONCAT( 'Happy', ' coding' ), ' together' ) FROM dual;

SELECT CONCAT(FIRST_NAME,' ',LAST_NAME) FROM EMPLOYEES

SELECT CONCAT( CONCAT( FIRST_NAME, ' ' ), LAST_NAME )


FROM EMPLOYEES;

You might also like