Lab Task 4
Lab Task 4
Faisalabad Campus
Database Systems
(Ms. Ayesha Ayub)
Topic:
“Using single-row character functions to customize output, use of
NVL function”
Lab Task 4
Q1:
Solve the Task
Answer:
Functions like UPPER() and LOWER() allow for converting strings to uppercase or lowercase,
respectively, making it easier to standardize data formats. The TRIM() function is crucial for
removing unwanted spaces from the beginning and end of strings, ensuring clean data
entries. Meanwhile, SUBSTR() and INSTR() enable users to extract specific substrings and
find the position of substrings within strings, providing greater control over string handling.
The CONCAT() function is used to join multiple strings into a single output, facilitating the
creation of full names or combined data fields. Additionally, RPAD() can be employed to
right-pad strings to a specified length, enhancing visual alignment in outputs.
Code of the above mentions Function:
create database ram;
use ram;
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
salary DECIMAL(10, 2),
hire_date DATE
);
SELECT
employee_id,
CONCAT(UPPER(IFNULL(first_name, 'Unknown')), ' ', UPPER(IFNULL(last_name,
'Unknown'))) AS full_name
FROM
employees;
SELECT employee_id,first_name,salary,MOD(salary, 1000) AS salary_modulo FROM
employees;
SELECT employee_id,first_name,salary,TRUNCATE(salary, 0) AS truncated_salary FROM
employees;
SELECT employee_id,first_name,salary,ROUND(salary, 1) AS rounded_salary FROM
employees;
SELECT employee_id,first_name,email,INSTR(email, '@') AS at_position FROM employees;
SELECT employee_id,first_name,email,RPAD(IFNULL(email, 'No Email Available'), 30, '*') AS
padded_email FROM employees;
SELECT employee_id,TRIM(IFNULL(first_name, 'Unknown')) AS
trimmed_first_name,TRIM(IFNULL(last_name, 'Unknown')) AS trimmed_last_name FROM
employees;
SELECT
employee_id,
LOWER(email) AS lowercase_email
FROM
employees;