0% found this document useful (0 votes)
12 views17 pages

String Built in Functions - Notes Lyst8400

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

String Built in Functions - Notes Lyst8400

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

Built In Functions

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:

1. Query the first name of all the employees in uppercase

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

2. Query the last name of all the employees in lowercase

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

3. Display all the letters of ‘john’ in upper case


SELECT UPPER('john');

Output:
UPPER(‘john’)

JOHN

4. Query the length of the string ‘Ronaldo’


To retrieve the length of the string you can make use of length()

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

kelly davis [email protected]

tom taylor [email protected]

mike whalen [email protected]

andy lumb [email protected]

anjel nair [email protected]

ram kumar [email protected]

rohan sharma [email protected]

john king jo__%%[email protected]

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

[email protected]

[email protected]

7
[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

john_king_jo__%%[email protected]

9. Query the substring from the string ‘RONALDO’ FROM 2nd position
and extract 5 characters

This can be achieved using SUBSTRING(). The syntax of substring() is


shown below

SELECT SUBSTRING("STRING", starting, length) AS


ExtractString;
OR
SUBSTRING(string FROM start FOR length)

This function returns the specified number of characters from a particular


position of a given string.
SELECT SUBSTRING('RONALDO', 2, 5) as sub;

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

You might also like