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.
📖 Concept Recap: String & Date Functions in SQLite
String functions:
UPPER(str)/LOWER(str)— change caseLENGTH(str)— number of charactersSUBSTR(str, start, len)— extract substring (1-indexed)TRIM(str)— remove leading/trailing whitespaceREPLACE(str, find, replace)— substitute textINSTR(str, substr)— position of first occurrence (0 = not found)str1 || str2— concatenate strings with the||operator
Date functions:
strftime('%Y', date)— extract year;'%m'= month,'%d'= dayjulianday(date)— convert to Julian day number for arithmeticCAST((julianday('now') - julianday(hire_date)) / 365 AS INTEGER)— years of serviceCASE WHEN condition THEN value ELSE other END— conditional labels
👀 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;
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.
SUBSTR(name, INSTR(name,' ')+1). The space is at position INSTR(name,' ').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).
CAST((julianday('2026-04-02') - julianday(hire_date)) / 365 AS INTEGER) gives years. Use this in CASE WHEN conditions.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'.
' - ': CAST(MIN(e.salary) AS TEXT) || ' - ' || CAST(MAX(e.salary) AS TEXT)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 →