SQL string functions are used for string manipulation. The following functions are commonly used string functions:
1. CONCAT(): Return concatenated string
select concat('Ashok','Kumar') from dual;
CONCAT('AS
---------- AshokKumar
2. INSTR(): Return position of first occurrence of a substring within a string
select instr('ASHOKKUMAR','A') FROM DUAL;
INSTR('ASHOKKUMAR','A')
-----------------------
1
select instr('ASHOKKUMAR','A',3) from dual;
INSTR('ASHOKKUMAR','A',3)
-------------------------
9
The above example will give the give the position of 'A' within string 'ASHOKKUMAR' starting from 3rd character.
3. LOWER(): Converts a string to lowercase
select lower('Ashok Kumar') from dual;
LOWER('ASHO
-----------
ashok kumar
4. LENGTH(): Return the length of a string in bytes
select length('Ashok Kumar') from dual;
LENGTH('ASHOKKUMAR')
--------------------
11
5. REPLACE(): Replace occurences of a substring within a string with another substring
select replace('AshokKumar','Kumar','KUMAR') from dual;
REPLACE('A
----------
AshokKUMAR
6. REVERSE(): Reverse all the characters in a string
select reverse('Ashok Kumar') from dual;
REVERSE('AS
-----------
ramuK kohsA
7. LPAD(): Return the string, left-padded with the specified string
select lpad('Ashok Kumar',15,'*') from dual;
LPAD('ASHOKKUMA
---------------
****Ashok Kumar
In the above example the total length of the returned string will be 15
8. LTRIM(): Removes leading spaces from a string
select ltrim(' Ashok Kumar') from dual;
LTRIM('ASHO
-----------
Ashok Kumar
9. SUBSTR(): Return the specified substring within a string
select substr('Ashok Kumar',4,2) from dual;
SU
--
ok
In the above example 4 is the starting position from a string and 2 is the total number of characters to be selected
10. RPAD(): Return the string, right-padded with the specified string
select rpad('Ashok Kumar',15,'*') from dual;
RPAD('ASHOKKUMA
---------------
Ashok Kumar****
11. RTRIM(): Removes trailing spaces from a string
select rtrim('Ashok Kumar ') from dual;
RTRIM('ASHO
-----------
Ashok Kumar
12. TRIM(): Removes leading and trailing spaces from a string
select trim(' Ashok Kumar ') from dual;
TRIM('ASHOK
-----------
Ashok Kumar
13. UPPER(): Converts a string to uppercase
select upper('Ashok Kumar') from dual;
UPPER('ASHO
-----------
ASHOK KUMAR
I hope the above examples in support of each string function will help you understand better.
1. CONCAT(): Return concatenated string
select concat('Ashok','Kumar') from dual;
CONCAT('AS
---------- AshokKumar
2. INSTR(): Return position of first occurrence of a substring within a string
select instr('ASHOKKUMAR','A') FROM DUAL;
INSTR('ASHOKKUMAR','A')
-----------------------
1
select instr('ASHOKKUMAR','A',3) from dual;
INSTR('ASHOKKUMAR','A',3)
-------------------------
9
The above example will give the give the position of 'A' within string 'ASHOKKUMAR' starting from 3rd character.
3. LOWER(): Converts a string to lowercase
select lower('Ashok Kumar') from dual;
LOWER('ASHO
-----------
ashok kumar
4. LENGTH(): Return the length of a string in bytes
select length('Ashok Kumar') from dual;
LENGTH('ASHOKKUMAR')
--------------------
11
5. REPLACE(): Replace occurences of a substring within a string with another substring
select replace('AshokKumar','Kumar','KUMAR') from dual;
REPLACE('A
----------
AshokKUMAR
6. REVERSE(): Reverse all the characters in a string
select reverse('Ashok Kumar') from dual;
REVERSE('AS
-----------
ramuK kohsA
7. LPAD(): Return the string, left-padded with the specified string
select lpad('Ashok Kumar',15,'*') from dual;
LPAD('ASHOKKUMA
---------------
****Ashok Kumar
In the above example the total length of the returned string will be 15
8. LTRIM(): Removes leading spaces from a string
select ltrim(' Ashok Kumar') from dual;
LTRIM('ASHO
-----------
Ashok Kumar
9. SUBSTR(): Return the specified substring within a string
select substr('Ashok Kumar',4,2) from dual;
SU
--
ok
In the above example 4 is the starting position from a string and 2 is the total number of characters to be selected
10. RPAD(): Return the string, right-padded with the specified string
select rpad('Ashok Kumar',15,'*') from dual;
RPAD('ASHOKKUMA
---------------
Ashok Kumar****
11. RTRIM(): Removes trailing spaces from a string
select rtrim('Ashok Kumar ') from dual;
RTRIM('ASHO
-----------
Ashok Kumar
12. TRIM(): Removes leading and trailing spaces from a string
select trim(' Ashok Kumar ') from dual;
TRIM('ASHOK
-----------
Ashok Kumar
13. UPPER(): Converts a string to uppercase
select upper('Ashok Kumar') from dual;
UPPER('ASHO
-----------
ASHOK KUMAR
I hope the above examples in support of each string function will help you understand better.
No comments:
Post a Comment