Learn Without Walls
← Back to SQL Labs
← Previous Lab Lab 8 of 10 — String & Date Functions Next Lab →

Lab 08 — String & Date Functions

Manipulate text and dates inside SQL. Format employee names, extract hire years, calculate tenure, and build conditional labels with CASE WHEN.

⏳ Loading SQL engine...
📋 AVAILABLE TABLES: departments, employees, projects, assignmentsdepartments(id, name, budget, location) employees(id, name, dept_id, salary, years, manager_id, hire_date) -- hire_date format: 'YYYY-MM-DD' e.g. '2019-03-15' projects(id, name, dept_id, budget, status, start_date) assignments(employee_id, project_id, hours_per_week, role)

📖 Concept Recap: String & Date Functions in SQLite

String functions:

Date functions:

👀 Worked Example (read-only)

Split employee names into first and last name parts using SUBSTR and INSTR.

SELECT name,
       UPPER(name) AS upper_name,
       LENGTH(name) AS name_len,
       SUBSTR(name, 1, INSTR(name, ' ') - 1) AS first_name,
       SUBSTR(name, INSTR(name, ' ') + 1) AS last_name
FROM employees;
✏ Guided

Exercise 1 — Name Formatting: "Last, First"

Extract first and last names, format as "Last, First" using concatenation (||), and derive a company email address. Fill in the blank for the last_name expression.

💡 Hint: last_name = SUBSTR(name, INSTR(name,' ')+1). The space is at position INSTR(name,' ').
💪 Independent

Exercise 2 — Hire Year, Tenure, and Tenure Category

Extract the hire year using strftime, calculate years of service using julianday, and assign a tenure category with CASE WHEN: 'New' (less than 2 years), 'Established' (2–5 years), 'Senior' (more than 5 years).

💡 Hint: CAST((julianday('2026-04-02') - julianday(hire_date)) / 365 AS INTEGER) gives years. Use this in CASE WHEN conditions.
🔥 Challenge

Exercise 3 — Department Summary with Formatted Strings

For each department show: name in UPPER case, location in LOWER case, headcount, budget formatted as '$' + value, average salary, and the salary range as a single string like '69000 - 115000'.

💡 Hint: Concatenate MIN and MAX with ' - ': CAST(MIN(e.salary) AS TEXT) || ' - ' || CAST(MAX(e.salary) AS TEXT)
🏆 Mini Project

Mini Project — Employee Anniversary Report

Build a work anniversary report for every employee showing: formal name (Last, First), hire date, hire year, years of service, next anniversary date in 2026, and a congratulations message using CASE WHEN.

Message rules: less than 1 year = 'Welcome!', exactly 2 years = '2-Year Anniversary!', exactly 5 years = '5-Year Anniversary!', exactly 10 years = '10-Year Anniversary!', otherwise = 'Milestone: X years'.

🏁 Lab 08 Complete!

You can now manipulate text with UPPER, LOWER, SUBSTR, INSTR, and string concatenation, extract date parts with strftime, calculate tenure with julianday, and build conditional labels with CASE WHEN.

Next: stop just reading data — start writing it.

Continue to Lab 09 — INSERT, UPDATE, DELETE & CREATE →