String Built in Functions - Notes Lyst8400
String Built in Functions - Notes Lyst8400
MySQL can do much more than just store and retrieve data. We can also
perform manipulations on the data before retrieving or saving it. That’s
where MySQL Functions come in. Functions are simply pieces of code that
perform some operations and then return a result. Some functions accept
parameters while other functions do not accept parameters.
Types Of Functions:
- String Functions
- Numeric Functions
- Date Functions
- Control Flow Function
- Conversion Function
- Aggregate Function
String Functions:
Now to convert the string to uppercase you can use of upper() function as
shown below
UPPER(str)
The function changes all characters of the specified str string to uppercase
and outputs the result.
SELECT
upper(first_name) as firstname
FROM
employee ;
1
Output:
firstname
KELLY
TOM
MIKE
ANDY
ANJEL
RAM
ROHAN
JOHN
Now to convert the string to lowercase you can use of lower() function as
shown below
LOWER(str)
The function changes all characters of the specified str string to lowercase
and outputs the result.
SELECT
LOWER(last_name) as lastname
FROM
employee ;
2
Output:
lastname
davis
taylor
whalen
lumb
nair
kumar
sharma
king
Output:
UPPER(‘john’)
JOHN
Syntax:
3
SELECT LENGTH(string) as alias;
SELECT LENGTH('Ronaldo');
Output:
LENGTH('Ronaldo')
5. Query the first name of all the employees along with their size
SELECT
first_name, length(first_name) as len
FROM
employee;
Output:
first_name len
kelly 5
tom 3
mike 4
andy 4
anjel 5
ram 3
rohan 5
4
john 4
6. Query firstname, salary of all employees who are earning 5 digits salary
without using like operator
SELECT
first_name, salary
FROM
employee
WHERE
length(salary) =5;
Output:
first_name salary
kelly 78000
tom 84200
mike 98200
andy 42200
anjel 42200
ram 64200
rohan 84200
7. Write a query to concatenate first name and last name of all employee
without using concatenation operator
For this you can make use of CONCAT(). This function is beneficial
when we need to concatenate or merge two or more strings or words.
5
SELECT
CONCAT(first_name, last_name) as details
FROM employee;
Output:
details
kellydavis
tomtaylor
mikewhalen
andylumb
anjelnair
ramkumar
rohansharma
johnking
8. Query first name, last name, email id of all the employees separated by
spaces
SELECT
CONCAT(first_name, ' ', last_name,' ', email) as details
FROM employee;
Output:
6
details
You can achieve the same using CONCAT_WS(). The syntax is as shown
below
CONCAT_WS(separator,str1,str2,...)
SELECT
CONCAT_WS('_',first_name, last_name, email) as details
FROM employee;
Output:
details
john_king_jo__%%[email protected]
9. Query the substring from the string ‘RONALDO’ FROM 2nd position
and extract 5 characters
Output:
sub
ONALD
8
10.Query the first name of all the employees along with first character of
first name
SELECT
first_name,SUBSTRING(first_name, 1,1) as sub
FROM
employee ;
Output:
first_name sub
kelly k
tom t
mike m
andy a
anjel a
ram r
rohan r
john j
9
11.Query the first name along with last character of first name
SELECT
first_name,SUBSTRING(first_name, length(first_name)) as
sub
FROM
employee ;
OR
SELECT
first_name,SUBSTRING(first_name, -1) as sub
FROM
employee ;
Output:
first_name sub
kelly y
tom m
mike e
andy y
anjel l
ram m
rohan n
john n
10
11
12.Query the first name of all the employees whose first character begins
with ‘R’ without using like operator
SELECT
first_name
FROM
employee
WHERE
SUBSTRING(first_name, 1,1) = 'r';
Output:
first_name
ram
rohan
13.Query the first name of all the employees whose first character is a vowel
SELECT
first_name
FROM
employee
WHERE SUBSTRING(first_name, 1,1) IN
('a','e', 'i', 'o', 'u');
Output:
first_name
andy
anjel
12
14.Query the last name of all the employees whose last but 1 character is not
a vowel
SELECT
last_name
FROM
employee
WHERE
SUBSTRING(last_name, -2,1)
NOT IN ('a','e', 'i', 'o', 'u');
Output:
last_name
lumb
sharma
king
15.Query the first name of all the employees with first character in uppercase
SELECT
UPPER(SUBSTRING(first_name, 1,1)) ||
SUBSTRING(first_name, 2)
as firstname
FROM employee;
Output:
first_name
13
Kelly
Tom
Mike
Andy
Anjel
Ram
Rohan
John
Here first we are extracting the first character using substring() then for that
function we are calling upper() so that the first character will get converted to
uppercase. Next all other characters extracted using substring and concatenate
with a concatenation operator we can make use of CONCAT() as well.
16.Query the first name of all the employees with first character in lowercase
and remaining characters in uppercase
SELECT
LOWER(SUBSTRING(first_name, 1,1)) ||
UPPER(SUBSTRING(first_name, 2))
as firstname
FROM employee;
Output:
first_name
14
kELLY
tOM
mIKE
aNDY
aNJEL
rAM
rOHAN
jOHN
17.Query the first half characters of first name in uppercase and rest in
lowercase
SELECT
UPPER(SUBSTRING(first_name, 1,length(first_name)/2)) ||
LOWER(SUBSTRING(first_name, length(first_name)/2+1))
as FN
FROM employee;
Output:
FN
KELly
TOm
15
MIke
ANdy
ANJel
RAm
ROHan
JOhn
16
18.Query the characters from position 5 to 9 after concatenating first name
and last name
SELECT
SUBSTRING(first_name || last_name, 5, 5) as F_N
FROM employee;
Output:
F_N
ydavi
aylor
whale
lumb
lnair
umar
nshar
king
17