02 Perform String Functions in MySQL
02 Perform String Functions in MySQL
Objective: To demonstrate how to use string functions for modifying the text data
in MySQL
Prerequisites: None
Steps to be followed:
1. Log in to the MySQL Database
2. Create a Database and Table
3. Use the CONCATENATE function
4. Use the LENGTH function
5. Use the Uppercase function
6. Use the SUBSTRING function
7. Use the TRIM function
8. Use the REPLACE function
9. Use the CHAR_LENGTH function
3.1 Combine the first_name and last_name columns into a full name for
each employee using the CONCAT() function
SQL Code:
SELECT emp_id, CONCAT(first_name, ' ', last_name) AS full_name FROM
employee_info;
4.1 Write a SELECT query using the LENGTH() function to determine the length
of the first_name and last_name strings for each employee
SQL Code:
SELECT emp_id, LENGTH(first_name) AS first_name_length,
LENGTH(last_name) AS last_name_length FROM employee_info;
Step 5: Use the Uppercase function
5.1 Convert the first_name and last_name strings to uppercase for each
employee
SQL Code:
SELECT emp_id, UPPER(first_name) AS upper_first_name,
UPPER(last_name) AS upper_last_name FROM employee_info;
Step 6: Use the SUBSTRING function
6.1 Extract a substring from the first_name column for each employee using
the SUBSTRING() function
SQL Code:
SELECT emp_id, SUBSTRING(first_name, 1, 2) AS substring_first_name
FROM employee_info;
7.1 Remove leading and trailing spaces from the first_name column for each
employee using the TRIM() function
SQL Code:
SELECT emp_id, TRIM(BOTH ' ' FROM first_name) AS trimmed_first_name
FROM employee_info;
Step 8: Use the REPLACE function
8.1 Replace occurrences of the letter 'o' with 'X' in the first_name column for
each employee using the REPLACE() function
SQL Code:
SELECT emp_id, REPLACE(first_name, 'o', 'X') AS replaced_first_name
FROM employee_info;
Step 9: Use the CHAR_LENGTH function
9.1 Write a SELECT query using the CHAR_LENGTH() function to determine the
number of characters in the first_name column for each employee
SQL Code:
SELECT emp_id, CHAR_LENGTH(first_name) AS char_length_first_name
FROM employee_info;