SQL Assignment: Date & Conversion
Functions
🔷 Part A: Date Functions
Q1. Display the current date and time:
SELECT CURRENT_TIMESTAMP AS current_datetime;
Q2. Insert employee records and perform date-based operations:
CREATE TABLE staff (
staff_id INT,
staff_name VARCHAR(60),
hire_date DATE,
dob DATE
);
INSERT INTO staff VALUES
(101, 'Imran', '2022-07-01', '1990-07-15'),
(102, 'Hina', '2018-04-10', '1992-04-20'),
(103, 'Omar', '2020-11-23', '1988-11-05'),
(104, 'Sana', '2019-02-18', '1994-02-28'),
(105, 'Tariq', '2021-09-05', '1991-09-17');
a) Year of hire for each staff:
SELECT staff_name, YEAR(hire_date) AS hire_year FROM staff;
b) Days worked since hire:
SELECT staff_name, DATEDIFF(CURDATE(), hire_date) AS total_days FROM
staff;
Q3. Staff members with birthdays in the current month:
SELECT staff_name, dob FROM staff WHERE MONTH(dob) =
MONTH(CURDATE());
Q4. Days difference between two dates using DATEDIFF():
SELECT DATEDIFF('2025-11-15', '2025-03-01') AS day_difference;
Q5. Add 45 days to a specific date:
SELECT DATE_ADD('2025-06-01', INTERVAL 45 DAY) AS new_future_date;
🔷 Part B: Conversion Functions
Q6. Create table grades and convert VARCHAR marks to INTEGER:
CREATE TABLE grades (
student_id INT,
student_name VARCHAR(50),
score VARCHAR(5)
);
INSERT INTO grades VALUES
(1, 'Arham', '70'),
(2, 'Nimra', '82'),
(3, 'Usman', '91'),
(4, 'Mehak', '87'),
(5, 'Rehan', '79');
SELECT AVG(CAST(score AS SIGNED)) AS avg_score FROM grades;
Q7. Convert a float to string using CAST():
SELECT CAST(456.789 AS CHAR) AS str_output;
Q8. Extract only date or only time from DATETIME:
SELECT CONVERT(NOW(), DATE) AS date_only, CONVERT(NOW(), TIME) AS
time_only;
Q9. Round and convert a float to integer using CAST():
SELECT CAST(ROUND(99.6543) AS SIGNED) AS rounded_int;
Q10. Convert lowercase string to uppercase and then to integer:
SELECT CAST(UPPER('6789') AS UNSIGNED) AS converted_value;